Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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.

0
"attempt to call a userdata value" Kallisus 43 — 4y
0
I've fixed it. youtubemasterWOW 2741 — 4y
Ad

Answer this question