player.CharacterAdded:Connect(function(character)
-- there are other code above and under this, but assume that the event under fires
-- when it hits a player
newHitbox.OnHit:Connect(function(hit, hitHum)
print("hit" .. slashType.Value .. " Client")
print("fired damage to server")
print(hitHum.Parent.Name)
game.ReplicatedStorage.hitHuman:FireServer(hitHum, slashType.Value)
end)
end)
the remote event “hitHuman” fires as many times as there are character added, however i have to keep it within the character added to keep the script working when the player resets.
the problem is when the character is hit (event OnHit), when the script fires to the server to damage the player (event hitHuman) it fires it as many times as there are characters added, I want it to always fire once, instead of the amount of characters added.
Have you tried putting it in a server script? If you’re firing an event to the server, theres not much point as the code you’ve displayed will all work in a server script.
im making a fighting game, and i want the hitboxes to be client sided, so im recording the hitboxes and when it hits an enemy on a local script which then tells the server to do the damage (hitHuman)
well, i want to keep my game with client-sided combat, if you know another way to replicate client sided combat please tell me.
the problem is when the character is hit (event OnHit), when the script fires to the server to damage the player (event hitHuman) it fires it as many times as there are characters added, I want it to always fire once, instead of the amount of characters added.
Move your LocalScript to ReplicatedFirst then the script will only fire once per user and instead of using character added, use something like repeat task.wait() until player.Charatcer so that the OnHit event is only bound once.
as said, ive put “CharacterAdded” event as I want the script to continue working after a player has died or reset, as if I don’t the scripts will continue using the old humanoid and character. also, the script is a local script for a tool, so it would be great if it stayed in the tool.
There isn’t really a point in doing so, since whenever you die you get a new tool meaning a new script happens, so that means it can continue working every time they reset.
Then use a debounce to stop the Event being bound multiple times.
local debouce = false -- change this to a value stored in the player
player.CharacterAdded:Connect(function(character)
-- there are other code above and under this, but assume that the event under fires
-- when it hits a player
if debounce == false then
debounce = true
newHitbox.OnHit:Connect(function(hit, hitHum)
print("hit" .. slashType.Value .. " Client")
print("fired damage to server")
print(hitHum.Parent.Name)
game.ReplicatedStorage.hitHuman:FireServer(hitHum, slashType.Value)
end)
end
end)
Each call to Connect creates another RBXScriptConnection, hence multiple events being fired. You are leaking connections and need to close these on character removal…
player.CharacterAdded:Connect(function(character)
local cnx = newHitbox.OnHit:Connect(function(hit, hitHum) ... end)
-- Clean up the connection on character removal
player.CharacterRemoving:Once(function()
cnx:Disconnect()
end)
end)
this works for disconnecting it when the character is removed, however for some reason, when another player joins and their character is added, it adds to the number of times the remote event is fired
just realised what you’ve done here, and i’ve tried to replicate it with a boolValue stored in the character, however it seems the same problem still happens