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
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.