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

Why doesn't this loop work?

Asked by 10 years ago

This script puts a cop from ServerStorage into the workspace then sets a variable and then if the variable is 1 it loops a if statement that is the cops health is less then 100 it puts it back in ServerStorage and heals it.

local cop = game.ServerStorage.Fgt local p = 0 local function onClicked(hit) p = 1 hit.PlayerGui.ScreenGui.ThreatLevel.Text = "Caution!" cop.Parent = game.Workspace wait(8) hit.PlayerGui.ScreenGui.ThreatLevel.Text = "No Threat"
end game.Workspace.fgtbutton.ClickDetector.MouseClick:connect(onClicked) if p == 1 then while true do if game.workspace.Fgt.Humanoid.Health <100 then game.workspace.Fgt.Humanoid.Health = 100 game.workspace.Fgt.Parent = game.ServerStorage end end

end

That is my problem it doesn't move it or heal it ?

I think the problem lays in the "while true do" part but I am a beginner scripter, so I need help to figure why it does this ?

2 answers

Log in to vote
-1
Answered by 10 years ago

Try this:

local cop = game.ServerStorage.Fgt
local p = 0 
game.Workspace.fgtbutton.ClickDetector.MouseClick:connect(function(hit) 
    plr = hit.Parent.Name -- name of person who hit the button
    p = 1 
    game.Players:FindFirstChild(plr).PlayerGui.ScreenGui.ThreatLevel.Text = "Caution!"
    cop.Parent = game.Workspace 
    wait(8)
    game.Players:FindFirstChild(plr).PlayerGui.ScreenGui.ThreatLevel.Text = "No Threat"
end)

while true do -- do a loop to check constantly
    wait() -- add a wait in the loop so it doesnt crash
    if p == 1 then 
        if game.workspace:FindFirstChild('Fgt') then -- add an extra check to make sure he exists
            if game.workspace:FindFirstChild('Fgt'):FindFirstChild('Humanoid').Health <100 then -- we use findfirstchild incase he is not in workspace; some error happens.
                game.workspace.Fgt.Humanoid.Health = 100
                game.workspace.Fgt.Parent = game.ServerStorage
            end
        end
    end
end
0
Try this, and next time, use the LUA box thing... killerkill29 35 — 10y
0
Yeah sorry about not useing Lua Box zacman4 0 — 10y
0
If it worked, press "Accept Answer" killerkill29 35 — 10y
0
*Code Block killerkill29 35 — 10y
Ad
Log in to vote
-1
Answered by 10 years ago

Thanks your script around finding out how spawned him was over complicated

local cop = game.ServerStorage.Fgt 
local p = 0 
local function onClicked(hit) 
    p = 1 hit.PlayerGui.ScreenGui.ThreatLevel.Text = "Caution!" 
    cop.Parent = game.Workspace 
    wait(8) 
    hit.PlayerGui.ScreenGui.ThreatLevel.Text = "No Threat"
 end 
game.Workspace.fgtbutton.ClickDetector.MouseClick:connect(onClicked)

while true do -- do a loop to check constantly
    wait()
    if p == 1 then 
        if game.workspace:FindFirstChild('Fgt') then 
            if game.workspace:FindFirstChild('Fgt'):FindFirstChild('Humanoid').Health <100
        then                
        game.workspace.Fgt.Humanoid.Health = 100
                game.workspace.Fgt.Parent = game.ServerStorage
            end
        end
    end
end
-- credit to killerkill29 for helping me with the script loop.
0
exast same as below NinjoOnline 1146 — 10y

Answer this question