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

Script keeps running even if the condition isn't met anymore. How do i fix it?

To make everything clear, i don't want it to run just once. I want it to run non-stop while the player is touching the part and stop when he's not.

function onTouch(part)
        local check = part.Parent:FindFirstChild("Humanoid")
    while check ~= nil do
        wait(1)
        check.Parent.Humanoid.Health = check.Parent.Humanoid.Health - 1
    end
end
script.Parent.Touched:connect(onTouch)

1 comment

0

use a if statement

2 answers

✓ Accepted answer

I'm keeping tract of who's touching the part and running through them to damage.

local part=--part
local touching={}

local running=false
function has()
	if running==false then running=true
		while #touching>0 do
			for _,v in pairs(touching) do
				if v:FindFirstChild("Humanoid") then
					v.Humanoid:TakeDamage(10)
				end
			end
			wait()
		end
		running=false
	end
end
part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local good=true
		for _,v in pairs(touching) do
			if v==hit.Parent then
				good=false
			end
		end
		if good then
			table.insert(touching,hit.Parent)
			has()
		end
	end
end)
part.TouchEnded:Connect(function(hit)
	for i,v in pairs(touching) do
		if v==hit.Parent then
			table.remove(touching,i)
		end
	end
end)
if script.Parent.Touched then
	function ()
		--ur code here
	end
end

9 comments

0

i'm afraid it's only gonna run once (i've kinda already tried something similar) but i'll give it a shot anyway

0

ok try it. if it works thats really good, if not u can check the wiki for if statement

0

doesn't run at all, no errors in the output either

0

also, i've already tried doing it by using the if statement, it only runs of code once and i don't want that

0

wait are you trying to make the person loose health or gain it

0

lose

0

try replacing line 5 with check.Parent.Humanoid:TakeDamage (1)

0

Nope, same thing. I still lose hp when i'm outside of it

0

sorry i forgot to add a while loop in the above script. add while true do [above whole code] end

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