Firing remote events within a "CharacterAdded" event fires the remote event multiple times, how to work around it?

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.

(this is a local script within a tool)

Having a local script on startercharacterscripts also works

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)

tldr; has to be on a local script

Doing that sort of logic on the client side isn’t a smart idea as you would be making it very easy for hackers to exploit your game.

You also haven’t been particularly descriptive as to what your actual issue is, so perhaps you could elaborate?

Would my idea work? Since whenever the character dies. A new script clones and parents to the character .

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.

this is a local script inside a tool, which dictates the animations, etc. of the tool, is there a way to make it into a startercharacterscript?

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.

Well I don’t think there’s a way to do that, but like you can also use playeradded as it can fire once when someone joins.

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)

this is what i tried before hand, i also put a debounce on the listener event and it did not work.

Was the debouce a local variable inside the script?

add a debounce in the remote function?

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)
2 Likes

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

1 Like

bump bump bump bump bumping!!!

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