Every time I kill another player, it adds more than just 1 kill to my player, it’s a very annoying problem. Even after disconnecting the loop when it adds +1 to the kills, I still have this problem. Can any kind soul help me with this?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
ReplicatedStorage.SimulationHitbox.OnServerEvent:Connect(function(Player, Args1, Args2)
local Hit = Instance.new("Part", Player.Character.HumanoidRootPart)
Hit.CanCollide = false
Hit.Anchored = false
Hit.Massless = true
Hit.Size = Vector3.new(5, 5, 5)
Hit.CFrame = Args1.CFrame * CFrame.new(0, 0, -4)
local Weld = Instance.new("WeldConstraint", Player.Character.HumanoidRootPart)
Weld.Part0 = Player.Character.HumanoidRootPart
Weld.Part1 = Hit
local Params = OverlapParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {Player.Character}
local Connection
local HittedChars = {}
Connection = RunService.Heartbeat:Connect(function()
warn("ENABLED")
local Parts = workspace:GetPartsInPart(Hit, Params)
for _, Victim in Parts do
if Victim.Parent:FindFirstChildWhichIsA("Humanoid") and not table.find(HittedChars, Victim.Parent) then
if Victim.Parent ~= Player.Character then
table.insert(HittedChars, Victim.Parent)
Victim.Parent.Humanoid.Died:Connect(function()
--local PlayerVictim = Players:GetPlayerFromCharacter(Victim.Parent)
--PlayerVictim.leaderstats.Deaths.Value = PlayerVictim.leaderstats.Deaths.Value + 1
--PlayerVictim.leaderstats.Killstreak.Value = 0
Player.leaderstats.Kills.Value = Player.leaderstats.Kills.Value + 1
Player.leaderstats.Killstreak.Value = Player.leaderstats.Killstreak.Value + 1
warn("HITTED")
Connection:Disconnect()
Hit:Destroy()
Weld:Destroy()
end)
end
end
end
Connection:Disconnect()
Hit:Destroy()
Weld:Destroy()
warn("DISABLED")
end)
end)
local debounce = false
while task.wait() do
test()
end
function test()
if debounce then return end
debounce = true
print("Test")
task.wait(5)
debounce = false
end
If debounce is true (The function activated within the last 5 seconds) then it will just return aka stop the function before running the code.
I know how to do debouce… My question is if I should put it inside the .Died event, I tried it right now and didn’t work. About add players to a table, my hitbox does it, when it hits someone, it inserts the player to a table, so I didn’t understand very well.
local onCooldown = {}
-- this goes to the top of the script/out of the event
function cooldownHandler(player)
for i,v in onCooldown do
if v == player then
return false
end
end
task.spawn(function()
table.insert(onCooldown, player)
task.wait(2)
for i,v in onCooldown do
if v == player then
table.remove(onCooldown, i)
end
end
end)
return true
end
-- this goes anywhere you want it, I prefer keeping all my functions at the very bottom
Victim.Parent.Humanoid.Died:Connect(function()
if cooldownHandler(Victim) then
--local PlayerVictim = Players:GetPlayerFromCharacter(Victim.Parent)
--PlayerVictim.leaderstats.Deaths.Value = PlayerVictim.leaderstats.Deaths.Value + 1
--PlayerVictim.leaderstats.Killstreak.Value = 0
Player.leaderstats.Kills.Value = Player.leaderstats.Kills.Value + 1
Player.leaderstats.Killstreak.Value = Player.leaderstats.Killstreak.Value + 1
warn("HITTED")
Connection:Disconnect()
Hit:Destroy()
Weld:Destroy()
end
end)
-- this replaces your script, if they are in the table then it returns false and doesn't run the code
-- if they aren't, then it adds them and waits 2 seconds before removing them (even if they leave)
Bro it worked! I just had to use table.clear instead table.remove (you can’t remove instances, just by using numbers). Anyways, thanks alot for the help bro, I was struggling, you’re more than a kind soul, God bless you!