Jump to content

My event handler doesn't call my function


Recommended Posts

Hi guys,

I'm trying create a little script in Lua. It will create a hot dog tent on All Saint Hospital (Los Santos) after 18:00hrs (6:00pm). The script will create the tent, two chairs, a ped (vendor) and a mark. When the player enter on mark, I wanna call the function PlayerWannaFood(). Look:

-- Variables definitions 
OpenHour = { ["HotDogSaintHospital"] = 18 } 
  
SHOP_Objects = {} 
SHOP_Tents   = {} 
SHOP_Markers = {} 
SHOP_Peds    = {} 
  
ObjectWasCreated = false 
  
  
function PlayerWannaFood(source) 
    setElementFrozen(source, true) 
    setPedAnimation(source, "FOOD", "EAT_Burguer", 2000, false, false, false) 
    setElementHealth(source, getElementHealth(source) + 20) 
    setElementFrozen(source, false)  
end 
  
function OpenFoodTents() 
  
    GameTimeHour, GameTimeMinute = getTime() -- Get the current game time 
  
    if( GameTimeHour >= OpenHour["HotDogSaintHospital"] ) then -- If the current game time is more that the open hour, so open the tent 
        if(ObjectWasCreated == false) then 
            SHOP_Objects[1] = createObject(643, 1281.20, -1383.30, 12.80, 0, 0, 52)   -- Chairs #01 
            SHOP_Objects[2] = createObject(643, 1282.60, -1379.80, 12.80, 0, 0, 326)  -- Chairs #02 
            SHOP_Tents[1]   = createObject(1340, 1285.19, -1382.40, 13.69, 0, 0, 180) -- HotDog Tent 
            SHOP_Markers[1] = createMarker(1284, -1382.4, 12.5, "cylinder", 1, 255, 0, 0, 255) 
            addEventHandler("onMarkerHit", SHOP_Markers[1], PlayerWannaFood) 
            SHOP_Peds[1]    = createPed(168, 1286, -1382.4, 14, 90.0) 
            ObjectWasCreated = true 
        end 
    else 
        if(ObjectWasCreated == true) then 
            destroyElement(SHOP_Objects[1]) 
            destroyElement(SHOP_Objects[2]) 
            destroyElement(SHOP_Tents[1]) 
            destroyElement(SHOP_Markers[1]) 
            destroyElement(SHOP_Peds[1]) 
            ObjectWasCreated = false 
        end 
    end 
end 
addEventHandler("onResourceStart", root, function () 
    setTimer(OpenFoodTents, 60000, 0) 
end) 

* I'm not good in Lua, so if there's a idiot error, I'm sorry.

My problem: When the player enter on mark, the function is not called. Why?

One love,

Stanley Sathler.

Link to comment

try this:

-- Variables definitions 
OpenHour = { ["HotDogSaintHospital"] = 18 } 
  
SHOP_Objects = {} 
SHOP_Tents   = {} 
SHOP_Markers = {} 
SHOP_Peds    = {} 
  
ObjectWasCreated = false 
  
  
function PlayerWannaFood(player) -- source was already used, changed it to player 
    setElementFrozen(player, true) 
    setPedAnimation(player, "FOOD", "EAT_Burguer", 2000, false, false, false) 
    setElementHealth(player, getElementHealth(player) + 20) 
    setElementFrozen(player, false)  
end 
  
function OpenFoodTents() 
  
    GameTimeHour, GameTimeMinute = getTime() -- Get the current game time 
  
    if( GameTimeHour >= OpenHour["HotDogSaintHospital"] ) then -- If the current game time is more that the open hour, so open the tent 
        if(ObjectWasCreated == false) then 
            SHOP_Objects[1] = createObject(643, 1281.20, -1383.30, 12.80, 0, 0, 52)   -- Chairs #01 
            SHOP_Objects[2] = createObject(643, 1282.60, -1379.80, 12.80, 0, 0, 326)  -- Chairs #02 
            SHOP_Tents[1]   = createObject(1340, 1285.19, -1382.40, 13.69, 0, 0, 180) -- HotDog Tent 
            SHOP_Markers[1] = createMarker(1284, -1382.4, 12.5, "cylinder", 1, 255, 0, 0, 255) 
            addEventHandler("onMarkerHit", SHOP_Markers[1], PlayerWannaFood) 
            SHOP_Peds[1] = createPed(168, 1286, -1382.4, 14, 90.0) 
            ObjectWasCreated = true 
        end 
    else 
        if(ObjectWasCreated == true) then 
            destroyElement(SHOP_Objects[1]) 
            destroyElement(SHOP_Objects[2]) 
            destroyElement(SHOP_Tents[1]) 
            destroyElement(SHOP_Markers[1]) 
            destroyElement(SHOP_Peds[1]) 
            ObjectWasCreated = false 
        end 
    end 
end 
  
addEventHandler("onResourceStart", resourceRoot, function () 
    setTimer(OpenFoodTents, 60000, 1) 
end) 

Link to comment

I fount the reason,

You spelled burger wrong:

-- Variables definitions 
OpenHour = { ["HotDogSaintHospital"] = 18 } 
  
SHOP_Objects = {} 
SHOP_Tents   = {} 
SHOP_Markers = {} 
SHOP_Peds    = {} 
  
ObjectWasCreated = false 
  
  
function PlayerWannaFood(player) -- source was already used, changed it to player 
    setElementFrozen(player, true) 
    setPedAnimation(player,"FOOD","EAT_Burger", 2000, false, false, false) -- You spelled burger wrong 
    setElementHealth(player,getElementHealth(player)+20) 
    setTimer(setElementFrozen,5000,1,player, false)  
end 
  
function OpenFoodTents() 
  
    GameTimeHour, GameTimeMinute = getTime() -- Get the current game time 
  
    if( GameTimeHour >= OpenHour["HotDogSaintHospital"] ) then -- If the current game time is more that the open hour, so open the tent 
        if(ObjectWasCreated == false) then 
            outputChatBox("Tent open") 
            SHOP_Objects[1] = createObject(643, 1281.20, -1383.30, 12.80, 0, 0, 52)   -- Chairs #01 
            SHOP_Objects[2] = createObject(643, 1282.60, -1379.80, 12.80, 0, 0, 326)  -- Chairs #02 
            SHOP_Tents[1]   = createObject(1340, 1285.19, -1382.40, 13.69, 0, 0, 180) -- HotDog Tent 
            SHOP_Markers[1] = createMarker(1284, -1382.4, 12.5, "cylinder", 1, 255, 0, 0, 255) 
            addEventHandler("onMarkerHit", SHOP_Markers[1], PlayerWannaFood) 
            SHOP_Peds[1] = createPed(168, 1286, -1382.4, 14, 90.0) 
            ObjectWasCreated = true 
        end 
    else 
        if(ObjectWasCreated == true) then 
            destroyElement(SHOP_Objects[1]) 
            destroyElement(SHOP_Objects[2]) 
            destroyElement(SHOP_Tents[1]) 
            destroyElement(SHOP_Markers[1]) 
            destroyElement(SHOP_Peds[1]) 
            ObjectWasCreated = false 
        end 
    end 
end 
  
addEventHandler("onResourceStart", resourceRoot,function() 
    setTimer(OpenFoodTents, 10000, 0) 
end) 

Link to comment

Hi Jaysds1,

I've been completing my code (I adding more tents in the city). When I enter on mark of first tent (All Saints Hospital, Los Santos) the function is called. But when I enter on the others markers around the city (I added a tent in Rodeo and Commerce), the function isn't called. The function to be called is the same. :/

-- Variables definitions 
HD_SaintHospital = { 
                        ["OpenHour"]   = 18, 
                        ["Tent"]       = nil, 
                        ["Marker"]     = nil, 
                        ["Object_1"]   = nil, 
                        ["Object_2"]   = nil, 
                        ["PedVendor"]  = nil, 
                        ["WasCreated"] = false 
                    }   -- Hot Dog Tent (Local: All Saint Hospital, Los Santos) 
  
HD_Rodeo = { 
            ["OpenHour"]   = 9, 
            ["CloseHour"]  = 18, 
            ["Tent"]       = nil, 
            ["Marker"]     = nil, 
            ["PedVendor"]  = nil, 
            ["WasCreated"] = false 
            -- Hot Dog Tent (Local: Rodeo, Los Santos) 
            } 
             
HD_Commerce = { 
                ["OpenHour"]   = 9, 
                ["CloseHour"]  = 18, 
                ["Tent"]       = nil, 
                ["Marker"]     = nil, 
                ["Object_1"]   = nil, 
                ["PedVendor"]  = nil, 
                ["WasCreated"] = false 
                -- Hot Dog Tent (Local: Rodeo, Los Santos) 
                } 
  
  
function PlayerWannaFood(player) 
    setElementFrozen(player, true) 
    setPlayerMoney(player, getPlayerMoney(player) - 5) 
    setPedAnimation(player, "FOOD", "EAT_Burger", 5000, false, false, true, false) 
    setElementHealth(player, getElementHealth(player) + 20) 
    setElementFrozen(player, false) 
end 
  
function TentDamageProof(carTarget) --Only for vehicle tents 
    setVehicleLocked(carTarget, true) 
    --setVehicleFuelTankExplodable(carTarget, false) 
    setVehicleDamageProof(carTarget, true) 
end 
  
function VendorInvencible() 
    cancelEvent() 
end 
addEventHandler("onClientPedDamage", getRootElement(), VendorInvencible) 
  
function OpenFoodTents() 
  
    GameTimeHour, GameTimeMinute = getTime() -- Get the current game time 
     
    -- All Saint Hospital 
    if( GameTimeHour >= HD_SaintHospital["OpenHour"] ) then -- If the current game time is more that the open hour, so open the tent 
        if(HD_SaintHospital["WasCreated"] == false) then 
            HD_SaintHospital["Object_1"] = createObject(643, 1281.20, -1383.30, 12.80, 0, 0, 52)   -- Chairs #01 
            HD_SaintHospital["Object_2"] = createObject(643, 1282.60, -1379.80, 12.80, 0, 0, 326)  -- Chairs #02 
            HD_SaintHospital["Tent"]     = createObject(1340, 1285.19, -1382.40, 13.69, 0, 0, 180) -- HotDog Tent 
            HD_SaintHospital["Marker"]   = createMarker(1284, -1382.4, 12.5, "cylinder", 1, 255, 0, 0, 255) 
            addEventHandler("onMarkerHit", HD_SaintHospital["Marker"], PlayerWannaFood) 
            HD_SaintHospital["PedVendor"]  = createPed(168, 1286, -1382.4, 14, 90.0) 
            HD_SaintHospital["WasCreated"] = true 
        end 
    else 
        if(HD_SaintHospital["WasCreated"] == true) then 
            destroyElement(HD_SaintHospital["Object_1"]) 
            destroyElement(HD_SaintHospital["Object_2"]) 
            destroyElement(HD_SaintHospital["Tent"]) 
            destroyElement(HD_SaintHospital["Marker"]) 
            destroyElement(HD_SaintHospital["PedVendor"]) 
            HD_SaintHospital["WasCreated"] = false 
        end 
    end 
     
     
    -- Rodeo 
    if( GameTimeHour >= HD_Rodeo["OpenHour"] ) then -- If the current game time is more that the open hour, so open the tent 
        if(HD_Rodeo["WasCreated"] == false) then 
            HD_Rodeo["Tent"]     = createObject(1340, 408, -1563.69, 27.7, 0, 0, 0) -- HotDog Tent 
            HD_Rodeo["Marker"]   = createMarker(409.20, -1563.69, 26.5, "cylinder", 1, 255, 0, 0, 255) 
            addEventHandler("onMarkerHit", HD_Rodeo["Marker"], PlayerWannaFood) 
            HD_Rodeo["PedVendor"]  = createPed(168, 407, -1563.69, 28, 270.0) 
            HD_Rodeo["WasCreated"] = true 
        end 
    else 
        if(HD_Rodeo["WasCreated"] == true) then 
            destroyElement(HD_Rodeo["Tent"]) 
            destroyElement(HD_Rodeo["Marker"]) 
            destroyElement(HD_Rodeo["PedVendor"]) 
            HD_Rodeo["WasCreated"] = false 
        end 
    end 
     
     
    -- Commerce 
    if( GameTimeHour >= HD_Commerce["OpenHour"] ) then -- If the current game time is more that the open hour, so open the tent 
        if(HD_Commerce["WasCreated"] == false) then 
            HD_Commerce["Tent"]     = createVehicle(588, 1098.30, -1775.5, 13.30, 0, 0, 90, "SARC 000") -- HotDog Van 
            TentDamageProof(HD_Commerce["Tent"]) 
            HD_Commerce["Object_1"] = createObject(643, 1100.9, -1771.59, 12.80, 0, 0, 0) 
            HD_Commerce["Marker"]   = createMarker(1098.59, -1773.09, 12.30, "cylinder", 1, 255, 0, 0, 255) 
            addEventHandler("onMarkerHit", HD_Commerce["Marker"], PlayerWannaFood) 
            HD_Commerce["WasCreated"] = true 
        end 
    else 
        if(HD_Commerce["WasCreated"] == true) then 
            destroyElement(HD_Commerce["Tent"]) 
            destroyElement(HD_Commerce["Object_1"]) 
            destroyElement(HD_Commerce["Marker"]) 
            HD_Commerce["WasCreated"] = false 
        end 
    end 
     
     
end 
addEventHandler("onResourceStart", root, function () 
    setTimer(OpenFoodTents, 60000, 0) 
end) 

Can you help me again? Or another member, please? I'm not being lazy, I tried yesterday all night, but nothing. :x I'm not experienced on Lua but I'm trying learn.

Hugs,

Stanley Sathler.

Link to comment

ok, it might be the defined variable then...

If the event handler works for the first one and you tried making another function and it was not working, it might be the defined variables

EDIT: Ya, I can't find any solutions

Link to comment

All of them returns,

There's no error in the script

I just don't know why the event doesn't trigger,

I've tried copying and pasting the first onMarkerEvent and changed the marker that it's going to trigger for and nothing...

Everything looks good but the event isn't being triggered.

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...