My local script seems to fire a RemoteEvent back to the server twice

So I’m making a project where the server fires an event to the client, which then fires back to the server, ending the loop. I then realized that when I’m in testing mode with Server and Clients (this is an important detail) my client only prints out my “Debug” line once, but the server does it twice. This leads me to believe my client is sending the message back to the server twice, for some reason.
Maybe it’s lag or maybe just a little bug. The thing is, I don’t know.

LocalScript code related to that:

AttackEvent.OnClientEvent:Connect(function()
	print('CALLED THE CLIENT')
	local PickedAnim = Animations[math.random(1, #Animations)]
	local Track = Character.Humanoid.Animator:LoadAnimation(PickedAnim)
	Track:Play()
	Track:AdjustSpeed(2)
	wait(0.25)
	AttackEvent:FireServer(Character:WaitForChild('HumanoidRootPart').CFrame)
end)

Server Script code related to that:

local function Slash()
	if Db == false then
		Db = true
		AttackEvent:FireClient(Player)
	end
end

AttackEvent.OnServerEvent:Connect(function(Player, ReturnedCFrame)
	print('CALLED SERVER')
	Sounds.Slash:Play()
	Sounds.Slash.TimePosition = 0.39
	local HitCharacter = AttackModule.ray(ReturnedCFrame, 4, Player.Character or Player.CharacterAdded:Wait())
	if HitCharacter then
		local HitSound = Sounds.Kerplunk:Clone()
		HitSound.Parent = HitCharacter.HumanoidRootPart
		HitSound:Play()
		
		HitCharacter.Humanoid.Health -= 15
	end
	wait(2)
	Db = false
end)

Both scripts are inside of a Tool, by the way.

OUTPUT:
(server)
CALLED SERVER
CALLED SERVER
(client who fired the event)
CALLED CLIENT

Thanks in advance!

PS: I was just testing this out, and turns out that when I reset character, even if I’m doing normal testing, the more times I reset, the more times it fires back. Maybe it has something to do with the amount of characters in the server or something? I was testing with 2 clients, by the way. ANOTHER ADDITION HERE: I’m using a system where players leave their ragdolled bodies after resetting, and the “extra times” seem to disappear when the ragdoll vanishes. Maybe it has something to do with the amount of Tools existing inside of the workspace?

I would put a print in the slash function showing the player.userid, so you see how many times the ‘slash’ function is firing and which player fired it.

Well, I just did that. All it does is print my ID once, even when the double fire is happening. I guess that makes sense, considering that the local script is only being called once, and all that the “Slash” function does is call it.

What I’m now guessing is happening is: The server script inside of EVERY tool in the game is recieving my local script call, which means everything is happening for as many times as the many scripts recieve my call. PS: This leads me into a question: Is putting server scripts inside of tools bad optimization? Does it hurt the game’s performance a lot?

I think I got it: What if every tool has its own Remote Event? I mean, i believe that’d work, but is that bad practice or bad for optimization? PS: Turns out this worked!

If you want to have only a single remote, you need to have a server script outside of the tool, that handles the remote events, and can determine which player to fire on. You can also have this one server script call module scripts inside the tool or set attributes on the tool to initiate functions or events specific to that tool. As for bad practice or bad for optimization, I don’t see that it will cause you any trouble. Glad you got it worked out.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.