Jump to content

Stack Overflow, function gets infinite.


Fabio(GNR)

Recommended Posts

So i call a server event from clientside, but it repeats endless. Idk why.

This is what calls it: Client Side

function vehCreate() 
if guiGetVisible(grid_cars) == true then 
selectedGrid = grid_cars 
elseif guiGetVisible(grid_bikes) == true then 
selectedGrid = grid_bikes 
elseif guiGetVisible(grid_air) == true then 
selectedGrid = grid_air 
elseif guiGetVisible(grid_misc) == true then 
selectedGrid = grid_misc 
end 
if source == button_vehicle_create_warp then 
warp = true 
elseif source == button_vehicle_create then 
warp = false 
end 
selectedVeh1, selectedVeh2 = guiGridListGetSelectedItem(selectedGrid) 
if selectedVeh1 ~= -1 then 
 textItem = guiGridListGetItemText(selectedGrid,selectedVeh1,selectedVeh2) 
 modelVehID = getVehicleModelFromName(textItem) 
 if not modelVehID then 
  dataItem = guiGridListGetItemData(selectedGrid, selectedVeh1,selectedVeh2) 
   if not dataItem then 
    outputChatBox("Can't retrieve data and name isn't good") 
    else 
    triggerServerEvent("giveVehicle",localPlayer, dataItem, warp) 
    end 
 else 
 triggerServerEvent("giveVehicle",localPlayer, modelVehID, warp) 
 end 
end 
end 

Server :

addEvent("giveVehicle",true) 
  
function createVehicle(vehID,warp) 
 name = getPlayerName(source) 
 outputChatBox("i Am " .. name .. ".") 
x, y, z = getElementPosition(source) 
local rx, ry, rz = getElementRotation(source) 
local veh = createVehicle(vehID,x,y,z+1,rx,ry,rz) 
 if warp == true then 
 warpPedIntoVehicle(source,veh,0) 
 else 
 setElementPosition(source,x,y,z+3) 
end 
end 
addEventHandler("giveVehicle", getRootElement(), createVehicle) 

Solved, had my function named createVehicle, which created the loop. Topic can be closed.

Link to comment

Try this. Not sure though, I'd rather want to see the whole script.

Client-side

local selectedGrid = false 
local warp = false 
  
function vehCreate() 
    if guiGetVisible(grid_cars) == true then 
        selectedGrid = grid_cars 
    elseif guiGetVisible(grid_bikes) == true then 
        selectedGrid = grid_bikes 
    elseif guiGetVisible(grid_air) == true then 
        selectedGrid = grid_air 
    elseif guiGetVisible(grid_misc) == true then 
        selectedGrid = grid_misc 
    end 
     
    if source == button_vehicle_create_warp then 
        warp = true 
    elseif source == button_vehicle_create then 
        warp = false 
    end 
  
    selectedVeh1, selectedVeh2 = guiGridListGetSelectedItem(selectedGrid) 
  
    if selectedVeh1 ~= -1 then 
        textItem = guiGridListGetItemText(selectedGrid, selectedVeh1, selectedVeh2) 
        modelVehID = getVehicleModelFromName(textItem) 
        if not modelVehID then 
            dataItem = guiGridListGetItemData(selectedGrid, selectedVeh1, selectedVeh2) 
        if not dataItem then 
            outputChatBox("Can't retrieve data and name isn't good") 
        else 
            triggerServerEvent("giveVehicle", localPlayer, dataItem, warp) 
        end 
    else 
        triggerServerEvent("giveVehicle", localPlayer, modelVehID, warp) 
    end 
end 

Server-side

function createVehicle(vehID, warp) 
    local name = getPlayerName(source) 
    outputChatBox("I am " .. name .. ".") 
     
    local x, y, z = getElementPosition(source) 
    local rx, ry, rz = getElementRotation(source) 
    local veh = createVehicle(vehID, x, y, z + 1, rx, ry, rz) 
    if warp == true then 
        warpPedIntoVehicle(source, veh, 0) 
    else 
        setElementPosition(source, x, y, z + 3) 
    end 
end 
addEvent("giveVehicle", true) 
addEventHandler("giveVehicle", root, createVehicle) 

Link to comment

you overwrote the mta function 'createVehicle', and then you call it inside it again on line 7.

Rename the function to something else.

 function createVehicleBlabla(vehID, warp) 
        local name = getPlayerName(source) 
        outputChatBox("I am " .. name .. ".") 
        
        local x, y, z = getElementPosition(source) 
        local rx, ry, rz = getElementRotation(source) 
        local veh = createVehicle(vehID, x, y, z + 1, rx, ry, rz) 
        if warp == true then 
            warpPedIntoVehicle(source, veh, 0) 
        else 
            setElementPosition(source, x, y, z + 3) 
        end 
    end 
    addEvent("giveVehicle", true) 
    addEventHandler("giveVehicle", root, createVehicleBlabla) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...