Jump to content

[Help] Need help with a robbing script


Recommended Posts

Hello people, i'm new to lua and scripting

I'm trying to write a rob script.

I want , when a player targets a ped , a timer starts and after timer ends player receive a random amount of money.

This is what i wrote.

local shops = {
	{name="Clucking Bell", px=368.11337, py=-4.49257, pz=1001.85156, mx=368.12790, my=-6.02016, mz=1001.85156, dim=5, int=9, skin=167, rot=180},
}


function ped ()
	for index, shop in pairs(shops) do
		local ped = createPed(shop.skin, shop.px, shop.py, shop.pz)
		setElementInterior(ped, shop.int)
		setElementDimension(ped, shop.dim)
		setPedRotation(ped, shop.rot)
		setElementFrozen(ped, true)
		setElementData(ped,"shopPed",true)
	end
end
addEventHandler ( "onResourceStart", root , ped ) 

function onPlayerTarget ( targetElem )
	if ped then
        setPedAnimation ( targetElem, "ped", "handsup",-1, false, false, false, true)
		triggerEvent("robtimer",root,player)
    end
end
addEventHandler ( "onPlayerTarget", getRootElement(), onPlayerTarget )  


function givemoney (player)
	local ran = math.random(1000, 2000)
	givePlayerMoney(player, ran)
end
addEvent("robtimer",true)
addEventHandler("robtimer",root,givemoney)

after pointing gun at the ped this is what i get in console:

[2017-07-01 11:13:07] WARNING: Rob\s_rob.lua:22: Bad argument @ 'setPedAnimation' [Expected element at argument 1, got boolean] --(but the Ped Animation changes!)
[2017-07-01 11:13:13] WARNING: Rob\s_rob.lua:32: Bad argument @ 'givePlayerMoney' [Expected element at argument 1, got boolean]

 

i really would appreciate the help :)

Link to comment
local shops = {
	{name="Clucking Bell", px=368.11337, py=-4.49257, pz=1001.85156, mx=368.12790, my=-6.02016, mz=1001.85156, dim=5, int=9, skin=167, rot=180},
}


function ped ()
	for index, shop in pairs(shops) do
		ped = createPed(shop.skin, shop.px, shop.py, shop.pz)
		setElementInterior(ped, shop.int)
		setElementDimension(ped, shop.dim)
		setPedRotation(ped, shop.rot)
		setElementFrozen(ped, true)
		setElementData(ped,"shopPed",true)
	end
end
addEventHandler ( "onResourceStart", root , ped ) 

function onPlayerTarget ( targetElem )
	if targetElem == ped then
        setPedAnimation ( targetElem, "ped", "handsup",-1, false, false, false, true)
		setTimer( function() 
        	local ran = math.random(1000, 2000)
			givePlayerMoney(source, ran)
        end, 5000, 1)
    end
end
addEventHandler ( "onPlayerTarget", getRootElement(), onPlayerTarget )  

 

  • Like 1
Link to comment
2 hours ago, Rose said:

local shops = {
	{name="Clucking Bell", px=368.11337, py=-4.49257, pz=1001.85156, mx=368.12790, my=-6.02016, mz=1001.85156, dim=5, int=9, skin=167, rot=180},
}


function ped ()
	for index, shop in pairs(shops) do
		ped = createPed(shop.skin, shop.px, shop.py, shop.pz)
		setElementInterior(ped, shop.int)
		setElementDimension(ped, shop.dim)
		setPedRotation(ped, shop.rot)
		setElementFrozen(ped, true)
		setElementData(ped,"shopPed",true)
	end
end
addEventHandler ( "onResourceStart", root , ped ) 

function onPlayerTarget ( targetElem )
	if targetElem == ped then
        setPedAnimation ( targetElem, "ped", "handsup",-1, false, false, false, true)
		setTimer( function() 
        	local ran = math.random(1000, 2000)
			givePlayerMoney(source, ran)
        end, 5000, 1)
    end
end
addEventHandler ( "onPlayerTarget", getRootElement(), onPlayerTarget )  

 

This Error fixed: 

[2017-07-01 11:13:07] WARNING: Rob\s_rob.lua:22: Bad argument @ 'setPedAnimation' [Expected element at argument 1, got boolean]

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

But still i receive this error and no money adds to player: 

[2017-07-01 17:42:30] WARNING: Rob\s_rob.lua:23: Bad argument @ 'givePlayerMoney' [Expected element at argument 1, got nil]

 

Edited by deihim
Link to comment
3 hours ago, deihim said:

This Error fixed: 

[2017-07-01 11:13:07] WARNING: Rob\s_rob.lua:22: Bad argument @ 'setPedAnimation' [Expected element at argument 1, got boolean]

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

But still i receive this error and no money adds to player: 

[2017-07-01 17:42:30] WARNING: Rob\s_rob.lua:23: Bad argument @ 'givePlayerMoney' [Expected element at argument 1, got nil]

 

try this:

local shops = {
	{name="Clucking Bell", px=368.11337, py=-4.49257, pz=1001.85156, mx=368.12790, my=-6.02016, mz=1001.85156, dim=5, int=9, skin=167, rot=180},
}


function ped ()
	for index, shop in pairs(shops) do
		ped = createPed(shop.skin, shop.px, shop.py, shop.pz)
		setElementInterior(ped, shop.int)
		setElementDimension(ped, shop.dim)
		setPedRotation(ped, shop.rot)
		setElementFrozen(ped, true)
		setElementData(ped,"shopPed",true)
	end
end
addEventHandler ( "onResourceStart", root , ped ) 

function onPlayerTarget ( targetElem )
	if targetElem == ped then
        setPedAnimation ( targetElem, "ped", "handsup",-1, false, false, false, true)
        	local ran = math.random(1000, 2000)
			setTimer(givePlayerMoney, 5000, 1,source, ran)
    end
end
addEventHandler ( "onPlayerTarget", getRootElement(), onPlayerTarget )  

 

Link to comment

For future warnings like these: "boolean" means that it's not sure if something is there. So in pretty much all cases a simple "if" check fixes it. Example:

local veh = getPedOccupiedVehicle(player)

setVehicleNitroLevel(veh, 1.0)

[This will give a boolean error^]

local veh = getPedOccupiedVehicle(player)

if veh then

setVehicleNitroLevel(veh, 1.0)

end

[This won't give you any errors/warnings]

PS: Sometimes even if you see a boolean warning the script may work normal. Still it's better to fix it mainly because it's annoying :D but also because it slows down your server like any other error or warning. 

Link to comment

ok now i have this problem, if you shoot the ped it drops dead :|

and to prevent ped damage only option was clientside so i changed i to clientside:

local shops = {
	{name="Clucking Bell", px=368.11337, py=-4.49257, pz=1001.85156, mx=368.12790, my=-6.02016, mz=1001.85156, dim=5, int=9, skin=167, rot=180},
}

function cancelPedDamage (attacker)  -- i added this to prevent damage but it's not working
	cancelEvent()
end
addEventHandler ( "onClientPedDamage", getRootElement(), cancelPedDamage )

function ped ()
	for index, shop in pairs(shops) do
		ped = createPed(shop.skin, shop.px, shop.py, shop.pz)
		setElementInterior(ped, shop.int)
		setElementDimension(ped, shop.dim)
		setPedRotation(ped, shop.rot)
		setElementFrozen(ped, true)
		setElementData(ped,"shopPed",true)
	end
end
addEventHandler ( "onResourceStart", root , ped ) 

function onPlayerTarget ( targetElem )
	if isTimer(timer) then return end
	if targetElem == ped then
        setPedAnimation ( targetElem, "ped", "handsup",-1, false, false, false, true)
        	local ran = math.random(1000, 2000)
			timer=setTimer(payPlayer, 5000, 1,source, ran)
			exports["Info"]:sendMessage(source, "Robbery Started!", 0, 92, 92)
    end
end
addEventHandler ( "onPlayerTarget", getRootElement(), onPlayerTarget )  

function payPlayer (source,ran)
	givePlayerMoney(source,ran)
	
end

but still ped dies.

any solutions? :)

Link to comment

This code should work:

local shops = {
	{name="Clucking Bell", px=368.11337, py=-4.49257, pz=1001.85156, mx=368.12790, my=-6.02016, mz=1001.85156, dim=5, int=9, skin=167, rot=180},
}



function ped ()
	for index, shop in pairs(shops) do
		ped = createPed(shop.skin, shop.px, shop.py, shop.pz)
		setElementInterior(ped, shop.int)
		setElementDimension(ped, shop.dim)
		setPedRotation(ped, shop.rot)
		setElementFrozen(ped, true)
		setElementData(ped,"shopPed",true)
        triggerClientEvent("sendPed", ped, ped)
	end
end
addEventHandler ( "onResourceStart", root , ped ) 

function onPlayerTarget ( targetElem )
	if isTimer(timer) then return end
	if targetElem == ped then
        setPedAnimation ( targetElem, "ped", "handsup",-1, false, false, false, true)
        	local ran = math.random(1000, 2000)
			timer=setTimer(payPlayer, 5000, 1,source, ran)
			exports["Info"]:sendMessage(source, "Robbery Started!", 0, 92, 92)
    end
end
addEventHandler ( "onPlayerTarget", getRootElement(), onPlayerTarget )  

function payPlayer (source,ran)
	givePlayerMoney(source,ran)
	
end

-- CLIENT
addEvent("sendPed", true)
addEventHandler("sendPed", root, 
  	function( ped ) 
    	peds = ped 
    	
 		 addEventHandler ( "onClientPedDamage", getRootElement(), 
    		function()
        		if ( source == peds ) then 
          			cancelEvent()
          		end
    		end 
  		)
    end 
)

 

Link to comment
10 hours ago, Rose said:

This code should work:


local shops = {
	{name="Clucking Bell", px=368.11337, py=-4.49257, pz=1001.85156, mx=368.12790, my=-6.02016, mz=1001.85156, dim=5, int=9, skin=167, rot=180},
}



function ped ()
	for index, shop in pairs(shops) do
		ped = createPed(shop.skin, shop.px, shop.py, shop.pz)
		setElementInterior(ped, shop.int)
		setElementDimension(ped, shop.dim)
		setPedRotation(ped, shop.rot)
		setElementFrozen(ped, true)
		setElementData(ped,"shopPed",true)
        triggerClientEvent("sendPed", ped, ped)
	end
end
addEventHandler ( "onResourceStart", root , ped ) 

function onPlayerTarget ( targetElem )
	if isTimer(timer) then return end
	if targetElem == ped then
        setPedAnimation ( targetElem, "ped", "handsup",-1, false, false, false, true)
        	local ran = math.random(1000, 2000)
			timer=setTimer(payPlayer, 5000, 1,source, ran)
			exports["Info"]:sendMessage(source, "Robbery Started!", 0, 92, 92)
    end
end
addEventHandler ( "onPlayerTarget", getRootElement(), onPlayerTarget )  

function payPlayer (source,ran)
	givePlayerMoney(source,ran)
	
end

-- CLIENT
addEvent("sendPed", true)
addEventHandler("sendPed", root, 
  	function( ped ) 
    	peds = ped 
    	
 		 addEventHandler ( "onClientPedDamage", getRootElement(), 
    		function()
        		if ( source == peds ) then 
          			cancelEvent()
          		end
    		end 
  		)
    end 
)

 

ERROR: Server triggered clientside event sendPed, but event is not added clientside

Link to comment

You must add all peds into table to work for all peds in shops table

local shops = {
    {name="Clucking Bell", px=368.11337, py=-4.49257, pz=1001.85156, mx=368.12790, my=-6.02016, mz=1001.85156, dim=5, int=9, skin=167, rot=180},
}
local peds = {}


function ped ()
    for index, shop in pairs(shops) do
        local ped = createPed(shop.skin, shop.px, shop.py, shop.pz)
        peds[ped] = true
        setElementInterior(ped, shop.int)
        setElementDimension(ped, shop.dim)
        setPedRotation(ped, shop.rot)
        setElementFrozen(ped, true)
        setElementData(ped,"shopPed",true)
    end
end
addEventHandler ( "onResourceStart", root , ped ) 

function onPlayerTarget ( targetElem )
    if peds[targetElem] then
        setPedAnimation ( targetElem, "ped", "handsup",-1, false, false, false, true)
        setTimer(givePlayerMoney, 5000, 1, source, math.random(1000, 2000)) -- 5000ms = 5 seconds
    end
end
addEventHandler ( "onPlayerTarget", getRootElement(), onPlayerTarget )  
  • Like 1
Link to comment

Ok all of it solved now, i'm trying to show the timer to the player
server side: 

function onPlayerTarget ( targetElem )
	if isTimer(timer) then return end
	if targetElem == ped then
        setPedAnimation ( targetElem, "ped", "handsup",-1, false, false, false, true)
        	local ran = math.random(1000, 2000)
			timer=setTimer(payPlayer, 5000, 1,source, ran)
			exports["Info"]:sendMessage(source, "Robbery Started!", 0, 92, 92)
			triggerClientEvent(source,root,"StartTheTimer")
    end
end
addEventHandler ( "onPlayerTarget", getRootElement(), onPlayerTarget )  

and this is the client side:

function clientTimer () 
	theTimer = setTimer(function() end, 10000, 1)
	remaining, executesRemaining, totalExecutes = getTimerDetails(theTimer) 
    local sWidth,sHeight = guiGetScreenSize() 
    local minutes = remaining
    dxDrawText(..minutes,sWidth*0.95486, sHeight*0.81112, sWidth*0.00694, sHeight*0.01112,tocolor(160,160,160,255),0.6,"bankgothic","left","top",false,false,false) 
end 
  
addEvent ( "StartTheTimer", true ) 
addEventHandler ( "StartTheTimer", root, clientTimer )

i'm pretty sure somewhere something is wrong

Error => [2017-07-02 11:52:01] WARNING: Rob\s_rob.lua:24: Bad argument @ 'triggerClientEvent' [Expected string at argument 2, got root]

Edited by deihim
Link to comment

triggerClientEvent(source,root,"StartTheTimer")

to

triggerClientEvent(source,"StartTheTimer", source)

 

and the client is :

local sWidth,sHeight = guiGetScreenSize() 
function timer()
    if isTimer(theTimer) then
        local s = getTimerDetails(theTimer) / 1000
        dxDrawText(("%.2d:%.2d"):format((s / 1000)  / 60, (s / 1000) % 60),sWidth*0.95486, sHeight*0.81112, sWidth*0.00694, sHeight*0.01112,tocolor(160,160,160,255),0.6,"bankgothic","left","top",false,false,false) 
    elseif added then
        removeEventHandler("onClientRender", root, timer)
        added = false
    end
end
unction clientTimer () 
    if not isTimer(theTimer) then
        theTimer = setTimer(function() end, 10000, 1) -- 10000 in client and 5000 in server?!!
        if not added then
            addEventHandler("onClientRender", root, timer)
            added = true
        end
    end
end 
  
addEvent ( "StartTheTimer", true ) 
addEventHandler ( "StartTheTimer", root, clientTimer )

 

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