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

This script keeps making my game say "script running for long time kill scipt" why ?

Asked by 6 years ago

When ever I try testing my game in studio it tells me to kill this script. hy is it doing this ? its a local script inside starterplayerscripts

local status = game.ReplicatedStorage.InfoValue
local plr = game.Players.LocalPlayer

while true do 
    if status.Value=='Round Beginning!'
        then 
        wait(1.9)
        if game.Players.LocalPlayer.AFK.Value==1 then
                local torso = game.Players.LocalPlayer.Character:WaitForChild('HumanoidRootPart')
                torso.Parent = game.Lighting
                game.Players.LocalPlayer.AFK.Value = game.Players.LocalPlayer.AFK.Value + 9
                wait(2)
        end
    end
end

2 answers

Log in to vote
0
Answered by 6 years ago

The reason, is, if the status is not "round beginning", then there is no wait. Even if you had a

while true do
end

loop, it would still ask you to kill it. To fix this, we should use a .Changed event.

local status = game.ReplicatedStorage.InfoValue
local plr = game.Players.LocalPlayer

status.Changed:Connect(function()
    if status.Value=='Round Beginning!'
        then 
        wait(1.9)
        if game.Players.LocalPlayer.AFK.Value==1 then
                local torso = game.Players.LocalPlayer.Character:WaitForChild('HumanoidRootPart')
                torso.Parent = game.Lighting
                game.Players.LocalPlayer.AFK.Value = game.Players.LocalPlayer.AFK.Value + 9
                wait(2)
        end
    end
end)

That way, this only fires when necessary. If you find yourself using a infinite loop, try finding a event that you could use in that case if you can use events in that case.

Hope this helps!

Ad
Log in to vote
1
Answered by 6 years ago

It is cause u r hangin'

local status = game.ReplicatedStorage.InfoValue
local plr = game.Players.LocalPlayer

while wait() do 
    if status.Value=='Round Beginning!'
        then 
        wait(1.9)
        if game.Players.LocalPlayer.AFK.Value==1 then
                local torso = game.Players.LocalPlayer.Character:WaitForChild('HumanoidRootPart')
                torso.Parent = game.Lighting
                game.Players.LocalPlayer.AFK.Value = game.Players.LocalPlayer.AFK.Value + 9
                wait(2)
        end
    end
end

Answer this question