How do I fix function looping

Why is my function looping?

Event.OnServerEvent:Connect(function()

if Move == 1 then
	--Animation 1
end

if Move == 2 then
	--Animation 2
end

if Move == 3 then
	--Animation 3
end

if Move == 4 then
	--Animation 4
end


Handle.Touched:Connect(function(hit)
	local Humanoid = hit.Parent.Humanoid
	
	Humanoid:TakeDamage(Damage)
	Event:Disconnect()
end)

Im confused on what you are trying to do, what do you mean by ‘stop a remove event’?

It keeps looping…so how do I stop this?

once the event fires it goes through the entirety of the contents of the event

if its looping then you have a significant error in your code

Do you mean remote event?
Also can you send your code so we can help you

Event.OnServerEvent:Connect(function()

if Move == 1 then
	--Animation 1
end

if Move == 2 then
	--Animation 2
end

if Move == 3 then
	--Animation 3
end

if Move == 4 then
	--Animation 4
end


Handle.Touched:Connect(function(hit)
	local Humanoid = hit.Parent.Humanoid
	
	Humanoid:TakeDamage(Damage)
	Event:Disconnect()
end)

Can you send the code where you are firing that event to the server with Event:FireServer()?

Sorry for saying that, but this title don’t make any sense.

You can’t stop the remote events, if the remote event is be using like a loop, it’s a problem with the local script that is firing the remote event or the script that is connected with the remote event.

Oh I thought it was the remote sorry.

1 Like

Yo, can you show the code that is sending the remote event, because that is where your problem is

also you should have animations play locally instead of serversided.

I did but I will send again ig

Event.OnServerEvent:Connect(function()

if Move == 1 then
	--Animation 1
end

if Move == 2 then
	--Animation 2
end

if Move == 3 then
	--Animation 3
end

if Move == 4 then
	--Animation 4
end


Handle.Touched:Connect(function(hit)
	local Humanoid = hit.Parent.Humanoid
	
	Humanoid:TakeDamage(Damage)
	Event:Disconnect()
end)

Your “looping” problem may have to do with the Touched function firing super fast, which happens if you don’t add any checks or debounces.

He is saying the local script that make the RemoteEvent:FireServer()

no he means

Event:FireServer()

in one of ur local scripts

Sword.Equipped:Connect(function()
Mouse.Button1Down:Connect(function()
if Debounce == 0 then
Event:FireServer()
Debounce = 1
wait(1)
Debounce = 0
end
end)
end)

Thats the script receiving the event, not sending, send the script that has the line Event:FireServer()

I can’t see any problem here, you can show all the local script?

Up above I mentioned the problem here is the solution. Your touched function is firing super fast, so this makes it so that the humanoid can only be hit once.

local hits = {}
Handle.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")--Looking for a humanoid, this is neccessary because a part may touch it which is not a player
	if humanoid and hits[humanoid] == nil then --If it is a player, and the humanoid is not already in the table, this is the part to solve your problem
		Humanoid:TakeDamage(Damage)
		hits[humanoid] = true
	end
end)

edit: replied to the wrong person

What I would do is something like this:

local Equipped = false

Sword.Equipped:Connect(function()
	Equipped = true
end)

Sword.Unequipped:Connect(function()
	Equipped = false
end)

Mouse.Button1Down:Connect(function()
	if Equipped == true then
		if Debounce == 0 then
			Event:FireServer()
			Debounce = 1
			wait(1)
			Debounce = 0
		end
	end
end)