Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't this loop work?

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

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

4 comments

0

Try this, and next time, use the LUA box thing...

0

Yeah sorry about not useing Lua Box

0

If it worked, press "Accept Answer"

0

*Code Block

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.

1 comment

0

exast same as below

This discussion is preserved as it was. New answers, comments, and votes are closed.