Jump to content

Killingstreak not resetting upon respawn


marty000123

Recommended Posts

Hi there :)

function resetKillingstreak (player)
    local killstreaks[source] >= 0 = getElementData( player, "killstreaks[source]" )
    if ( killstreaks[source] >= 0 ) then
        --They have a killingstreak higher than 0
        --We will want to reset the element data back to its default (that being false)
        setElementData ( player, "killstreaks[source]", false )
end
addEventHandler("onPlayerWasted", getRootElement(), resetKillingstreak)

I have a killingstreak script, but it didn't reset when you respawned, so when you died and killed someone after spawning it would for example tell you you're on a 10 killing streak, while you should've lost your killingstreak obviously. So I've worked on the script given above, but it gives the following error: (found an unexpected symbol near ''['')

Now I'm not entirely sure what to do.. This isn't my first help request today, but I'm trying to learn LUA the best I can. So yeah help will be appreciated.

Marty

Edited by marty000123
Link to comment

[/lua]function resetKillingstreak (player)
    local killstreaks = getElementData( player, "killstreaks" )
    if tonumber( killstreaks ) then
        if ( killstreaks >= 0 ) then
        --They have a killingstreak higher than 0
        --We will want to reset the element data back to its default (that being false)
            setElementData ( player, "killstreaks", false )
           end
       else
           return
       end
end
addEventHandler("onPlayerWasted", getRootElement(), resetKillingstreak)[/lua]

Link to comment
10 hours ago, Anubhav said:

[/lua]function resetKillingstreak (player)
    local killstreaks = getElementData( player, "killstreaks" )
    if tonumber( killstreaks ) then
        if ( killstreaks >= 0 ) then
        --They have a killingstreak higher than 0
        --We will want to reset the element data back to its default (that being false)
            setElementData ( player, "killstreaks", false )
           end
       else
           return
       end
end
addEventHandler("onPlayerWasted", getRootElement(), resetKillingstreak)[/lua]

Thanks, you'll hear from me soon if it worked or not!

Link to comment

It doesn't reset. When I get killed after having a 3killing-streak it says that someone stopped my killing streak. If I respawn and if someone kills me, it again says that he stopped my killing-streak while I shouldn't have a killingstreak because I didn't kill anyone..

It does give me 3 errors: 

[color=black][2016-11-19 05:47:47] WARNING: killstreak\server.lua:16: Bad argument @ 'getElementData' [Expected element at argument 1, got nil]
[2016-11-19 05:47:47] ERROR: killstreak\server.lua:17: attempt to perform arithmetic on local 'exp' (a boolean value)
[2016-11-19 05:47:47] WARNING: killstreak\server.lua:41: Bad argument @ 'getElementData' [Expected element at argument 1, got number '1'][/color]

 

 

(for some reason the letters turned white so just mark the text to be able to read it)

The full script can be found here: http://pastebin.com/HhFzYpcz

Thanks for the help guys.

Link to comment

Is it the handler of onPlayerWasted? cause the player is not the first parameter but the source of the event if i remember right

EDIT: why are you resetting the elementData of the player to false if you store the data in a table O.o that doesn't make any sense. 

Let us know if it's working :)

Edited by LoPollo
Link to comment
1 hour ago, LoPollo said:

Is it the handler of onPlayerWasted? cause the player is not the first parameter but the source of the event if i remember right

EDIT: why are you resetting the elementData of the player to false if you store the data in a table O.o that doesn't make any sense. 

Let us know if it's working :)

I know it doesn't make much sense because I barely know how to script so I'm basically just putting loose parts together I found on the wiki :D

Therefor I'm not fully sure how to solve it even with your information. And does it fix the warnings/errors at the same time or is this a different problem?

  • Like 1
Link to comment
1 hour ago, marty000123 said:

. And does it fix the warnings/errors at the same time

What i said does NOT fix the errors above.

 

What i said is about the fact the killstreak is not resetted: you set to false some data of an element, but actually you store the killstreak in a table, not on the element itself.

--this is you func i was talking about:
function resetKillingstreak (player)
    local killstreaks = getElementData( player, "killstreaks" )
    if tonumber( killstreaks ) then
        if ( killstreaks >= 0 ) then
        --They have a killingstreak higher than 0
        --We will want to reset the element data back to its default (that being false)
            setElementData ( player, "killstreaks", false )
           end
       else
           return
       end
end

--but actually you store the data in
local killstreaks = {}

--so the function should work on that:

function resetKillingstreak ()
	local player = source --it's not the first parameter, it is the source!
	--local killstreaks = killstreaks[player]
	if killstreaks[player] and ( killstreaks[player] > 0 ) then --if the killstreak exists (it may be nil) and it's higher than 0
		--They have a killingstreak higher than 0
		--We will want to reset the element data back to its default (that being false)
		killstreaks[player] = 0 --set it to 0
	end
end

 

About the errors posted above, it's because you are trying to use a false value as a number:

getElementData returns false if the data or the element itself does not exists

since you are trying to get data that was never setted, it does not exists and the getElementData returns false

In the next line you add 1 to that value (that should be a number, but as explained it's false instead) and so it fails.

 

So to fix this you should handle the data that does not exist. The easiest way it to add an "or 0" when settin the variable: in case the function before the "or" returns a false value (false or nil) the value after the or is used instead: in your case it will be set to 0 instead with this fix, and not false if the get data return false.

--replace
local exp = getElementData(player, "experience")
--with
local exp = getElementData(player, "experience") or 0

You should replace this everytime the function is called and it may return false. Since you are only increasing that value by 1, you can only add the "or 0" on line 11

 

Hope i've been clear, let us know if it works ;) also good luck with learning lua

  • Like 1
Link to comment

Hi Pollo,

I really appreciate your help, senpai! But sadly it didn't work. I'm still having the same issue, it didn't help at all. The errors did go away, so thanks for that xD

But can you look into it again please? When I get killed twice in a row after having a 3-killing-streak, I still see the message that someone ended my killing-streak, and vice versa.

Link to comment

Ok it didn't work for me either and did not see any error... but found the problem:

when working with the experience data and outputchatboxes from line 10 to line 27 there's an undefined variable: "player". it should be "attacker" instead.

If you are lazy you can define that variable inserting at line 4: local player = attacker

 

Note: i used a modified version of the script, to triggers events via command, letting me setting the attacker and source, also beign alone in the server made me use some checks in the script to "simulate" the behaviour of the script with different players. So event it was tested it's better to test in a real environment, with more players.

Edited by LoPollo
Link to comment

I had no time, i even right now i do not have much time. So i'm posting the script i was playing on

local killstreaks = {}

function onPedWastedHandler( ammo, attacker, weapon, bodypart, loss )
	if attacker then
		local thePlayer = attacker
		if killstreaks[attacker] then
			killstreaks[attacker] = killstreaks[attacker] + 1
		else
			killstreaks[attacker] = 1
		end
		outputDebugString( killstreaks[attacker] )
		if killstreaks[attacker] == 3 then
			outputChatBox (getPlayerName(attacker).." got a 3 killing-streak! Nice! (500XP reward)",thePlayer, 0,255,0)
			local exp = getElementData(thePlayer, "experience") or 0
			setElementData(thePlayer, "experience", tonumber(exp+500))
		end
		if killstreaks[attacker] == 5 then
			outputChatBox (getPlayerName(attacker).." got a 5 killing-streak! Great! (1000XP reward)",thePlayer,0,255,0)
			local exp = getElementData(thePlayer, "experience")
			setElementData(thePlayer, "experience", tonumber(exp+1000))
		end
		if killstreaks[attacker] == 10 then
			outputChatBox (getPlayerName(attacker).." got a 10 killing-streak! Awesome! (2000XP reward)",thePlayer,0,255,0)
			local exp = getElementData(thePlayer, "experience")
			setElementData(thePlayer, "experience", tonumber(exp+2000))
		end
		if killstreaks[attacker] == 15 then
			outputChatBox (getPlayerName(attacker).." got a 15 killing-streak! RAMPAGE! (4000XP reward)",thePlayer,0,255,0)
			local exp = getElementData(thePlayer, "experience")
			setElementData(thePlayer, "experience", tonumber(exp+4000))
		end


		if killstreaks[source] then
			if killstreaks[source] >= 3 then
				outputChatBox (getPlayerName(attacker).." stopped the "..killstreaks[source] .." killing-streak of "..getPlayerName(source).."!",getRootElement (), 255,0,0)
			end
		end
	end
end
addEventHandler ('onPlayerWasted', root, onPedWastedHandler)
addEvent("testEvent", false)
addEventHandler( "testEvent", root, onPedWastedHandler)
 
function resetKillingstreak ()
	local player = source --it's not the first parameter, it is the source!
	--local killstreaks = killstreaks[player]
	if killstreaks[player] and ( killstreaks[player] > 0 ) then --if the killstreak exists (it may be nil) and it's higher than 0
		--They have a killingstreak higher than 0
		--We will want to reset the element data back to its default (that being false)
		killstreaks[player] = 0 --set it to 0
		outputDebugString( "reset" )
	end
end
addEventHandler("onPlayerWasted", root, resetKillingstreak)

addCommandHandler( "test2", 
	function(thePlayer,cmd,...)
		triggerEvent( "testEvent", thePlayer, 0, thePlayer )
		
	end
)

This is 3 days old and i did not touch it, it should work if i didn't make any more tests. This is the script i was using to control the attacker and the source of the deathHandler

I used the command to add kills (without actually killing, i was alone :( ) without dying, and suiciding with /kill resets the killstreak since also the resetKillingstreak function gets called. 

NOTE: i also renamed the variable player to thePlayer, but that was for another test i removed from here... so don't mind about that change

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