Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how to prevent the childremove function on Remotevent(Server Script) to stack?

i'm having problem with this script at first Activation its fine but on 2nd time u fire the remote the ChildRemove function stacks idk why it likes print 3x instead of 1 and makes my speed 0

local Debris = game:GetService("Debris")
game.ReplicatedStorage.Arm.OnServerEvent:Connect(function(Player, Arg)
if Arg == "Yeet" then
Print("Faster")
local a = Instance.new("Part", Player.Character)
a.Name = "Sped"
Debris:AddItem(a, 5)
Player.Character.Humanoid.WalkSpeed = 32
Player.Character.ChildRemoved:Connect(function(Removed)
if Removed.Name == "Sped" then
Print("Slowed")
Player.Character.Humanoid.WalkSpeed = Player.Character.Humanoid.WalkSpeed - 16
end
end)
end
end)

Output on First Activation

print("Slowed")

Output on 2nd activation

print("Slowed") 3x

1 answer

✓ Accepted answer

Hello. You have to use :Disconnect(). Try this:

local Debris = game:GetService("Debris")

game.ReplicatedStorage.Arm.OnServerEvent:Connect(function(Player, Arg)
	if Arg == "Yeet" then
		Print("Faster")

		local a = Instance.new("Part", Player.Character)
		a.Name = "Sped"

		Debris:AddItem(a, 5)

		Player.Character.Humanoid.WalkSpeed = 32

		local removed

		local function onChildRemoved()
			if Removed.Name == "Sped" then
				Print("Slowed")
				Player.Character.Humanoid.WalkSpeed = Player.Character.Humanoid.WalkSpeed - 16
				removed:Disconnect()
			end
		end

		removed = Player.Character.ChildRemoved:Connect(onChildRemoved)
	end
end)

The script will disconnect the event when the child named "Sped" is removed. Please accept and upvote this answer if it helped.

2 comments

0

"attempt to call a userdata value"

0

I've fixed it.

This discussion is preserved as it was. New answers, comments, and votes are closed.