I’m working on a script that essentially adds 1 to a killCount variable when onPlayerKilled is run. I want to make this only increase the value of the killCount on the killer’s client, which is not how it seems to be happening right now. Aside from that, the killCount variable also keeps increasing several times after the event is called, going to numbers like 800+ from one death.
Any suggestions on how to reverse the roles so that the variable increase happens for the killer? And any solutions to forcing the variable to only increase by 1 per call? Thanks guys!
local killCount = 0
Citizen.CreateThread(function()
while true do
Citizen.Wait(1000)
AddEventHandler("baseevents:onPlayerKilled", function(player, killer, reason, pos)
killCount = killCount + 1
end)
end
end)
textPos = {x = 0.005, y = 0.7}
rgb = {r = 255, g = 64, b = 64}
alpha = 255
size = 0.4
font = 1
Citizen.CreateThread(function()
while true do
Wait(1)
SetTextColour(rgb.r, rgb.g, rgb.b, alpha)
SetTextFont(font)
SetTextScale(size, size)
SetTextWrap(0.0, 1.0)
SetTextCentre(false)
SetTextDropshadow(2, 2, 0, 0, 0)
SetTextEdge(1, 0, 0, 0, 205)
SetTextEntry("STRING")
AddTextComponentString("Kills: ".. killCount)
DrawText(textPos.x, textPos.y)
end
end)
Event handlers don’t need to be in a loop! Remove the loop and the big number increase will be solved.
Are you running the onPlayerKilled handler on the server-side right? Looking at the handlers, there are 2 params that you need to look at. “player” = killedID and “killer” = killerID
local killCount = 0
RegisterNetEvent('kills:updateKill')
AddEventHandler('kills:updateKill', function(totalKill)
killCount = totalKill
end)
textPos = {x = 0.005, y = 0.7}
rgb = {r = 255, g = 64, b = 64}
alpha = 255
size = 0.4
font = 1
Citizen.CreateThread(function()
while true do
Wait(1)
SetTextColour(rgb.r, rgb.g, rgb.b, alpha)
SetTextFont(font)
SetTextScale(size, size)
SetTextWrap(0.0, 1.0)
SetTextCentre(false)
SetTextDropshadow(2, 2, 0, 0, 0)
SetTextEdge(1, 0, 0, 0, 205)
SetTextEntry("STRING")
AddTextComponentString("Kills: ".. killCount)
DrawText(textPos.x, textPos.y)
end
end)
server.lua
local PlayerKill = []
AddEventHandler("baseevents:onPlayerKilled", function(killerID, deathData)
local victim = source
if killerID ~= victim then
if PlayerKill[killerID] == nil then
PlayerKill[killerID] = 0
end
PlayerKill[killerID] = PlayerKill[killerID] + 1
TriggerClientEvent("kills:updateKill", killerID, PlayerKill[killerID])
else
-- Do something if suicide
end
end)