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

Leaking Threads? How could i prevent it?

Asked by 10 years ago
Edited 5 years ago

At the moment this will leak threads, how could I end the loop if the player is no longer in the game so that the script does not break.

Also I need to manually add the gui because of the way I will be using it so I cannot change this part!!

function onPlayerRespawn(property, plr)
        while true do
            if plr.Character ~= nil then break end
            wait(5)
        end
    print("Given gui")
    Workspace.ScreenGui:Clone().Parent = plr.PlayerGui
end

function onPlayerEntered(plr)
        while true do
            if plr.Character ~= nil then break end
            wait(5)
        end
        print("plr Added")
        Workspace.ScreenGui:Clone().Parent = plr.PlayerGui 
        plr.CharacterAdded:connect(function(property) onPlayerRespawn(property, plr) end)
end
game.Players.PlayerAdded:connect(onPlayerEntered)

Fixed

function onPlayerRespawn(property, plr)
        while plr and plr.Parent do
            if plr.Character then break end
            wait(1) --In my opinion, five seconds is too much. I'd just do wait(). It's up to you if you wan't to change that.
        end
        if not (plr and plr.Parent) then return end
    print("Given gui")
    if game.Workspace.FilteringEnabled then
        game.Workspace.FilteringEnabled = false
        wait(1)
        Workspace.ScreenGui:Clone().Parent = plr.PlayerGui
        wait(1)
        game.Workspace.FilteringEnabled = true
    else
        game.Workspace.FilteringEnabled = true
    end

end

function onPlayerEntered(plr)
        print("plr Added")
        plr.CharacterAdded:connect(function(property) onPlayerRespawn(property, plr) end)
end
game.Players.PlayerAdded:connect(onPlayerEntered)

2 answers

Log in to vote
1
Answered by
2eggnog 981 Moderation Voter
10 years ago

Your current script will copy the gui twice. But anyways, here is how you would stop the loop and end the function if the player is no longer in the game.

function onPlayerRespawn(property, plr)
        while plr and plr.Parent do
            if plr.Character then break end
            wait(5) --In my opinion, five seconds is too much. I'd just do wait(). It's up to you if you wan't to change that.
        end
        if not (plr and plr.Parent) then return end
    print("Given gui")
    Workspace.ScreenGui:Clone().Parent = plr.PlayerGui
end

function onPlayerEntered(plr)
        print("plr Added")
        plr.CharacterAdded:connect(function(property) onPlayerRespawn(property, plr) end)
end
game.Players.PlayerAdded:connect(onPlayerEntered)

Also, I'm not sure where the property variable is coming from. It's undefined, unless this isn't the entire script.

0
Thanks for the help, i also added filtering so that the new gui is sent to the client User#5423 17 — 10y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Lua local a = "This will now work as expected"

this shows as an underline

lua local a = "this will work as expected" this is bold text

(random code) this should hide after 15 lines of code

```lua repeat wait() until player.Character ~= nil

local hum = player.Character:WaitForChild("Humanoid")

local animation = Tool.Anim

local AnimTrack = hum:LoadAnimation(animation)

Tool.Activated:Connect(function(mouse)

AnimTrack:Play()

end)

Tool.Unequipped:Connect(function()

AnimTrack:Stop()

end) repeat wait() until player.Character ~= nil

local hum = player.Character:WaitForChild("Humanoid")

local animation = Tool.Anim

local AnimTrack = hum:LoadAnimation(animation)

Tool.Activated:Connect(function(mouse)

AnimTrack:Play()

end)

Tool.Unequipped:Connect(function()

AnimTrack:Stop()

end) ```

0
what User#24403 69 — 5y

Answer this question