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

How come the bool value isn't turning false after the time ends?

Hello, what I'm trying to do is create a jail tool. When the handle of the tool touches a player, he'll be moved to a specific part. The problem for this was that it kept putting the person in jail. I tried to put a Bool Value called "Arrest" that turns true if it touches someone. After eight seconds, it'll be false. What this does is that if the Bool Value is true then it won't teleport, but if it's false, then it'll teleport. The problem is that it won't work because the BoolValue is always staying at true and never turning false.

tool = script.Parent
Arrest = script.Parent.Arrest

function touch(hit)
h = hit.Parent
g = h:findFirstChild("Humanoid")
if Arrest.Value == false then
Arrest.Value = true
elseif Arrest.Value == true then 
Arrest.Value = false
if g ~= nil then
if g.Health > 0 then
h:MoveTo(game.workspace.Jail.Position)
wait(8)
Arrest.Value = false
end
end
end
end

function sel()
tool.Handle.Touched:connect(touch)
end

tool.Equipped:connect(sel)

1 comment

0

Are there any errors?

1 answer

✓ Accepted answer

I made a solution for you.

Firstly you made the script set the Arrest.Value to false if it where true:

if Arrest.Value == false then -- Arrest is false, setting arrest to true
Arrest.Value = true
elseif Arrest.Value == true then -- Arrest is true, setting arrest to false.
Arrest.Value = false
end

elseif Arrest.Value == true then makes it possible for the script to skip the 8 seconds wait. Since you called the function with an :connect() it would be called, and thus start from the top, again each time the event fired. That's the explanation on why you kept being thrown in jail.

I removed that part and the script should now work as intended

tool = script.Parent
Arrest = script.Parent.Arrest

function touch(hit)
h = hit.Parent
g = h:findFirstChild("Humanoid")
	if Arrest.Value == false then
	Arrest.Value = true
		if g ~= nil then
			if g.Health > 0 then	
				h:MoveTo(game.workspace.Jail.Position)
				wait(8) -- after 8 sec, set Arrest.Value to false
				Arrest.Value = false
			end
		end
	end
end

function sel()
tool.Handle.Touched:connect(touch)
end

tool.Equipped:connect(sel)

Besides that you had some misplaced code snippets, look at this part:

if Arrest.Value == false then
Arrest.Value = true
elseif Arrest.Value == true then 
Arrest.Value = false
if g ~= nil then
if g.Health > 0 then
h:MoveTo(game.workspace.Jail.Position)
wait(8)
Arrest.Value = false
end
end
end

You told Arrest.Value to be false twice, because you put the if g ~= nil then part inside the elseif Arrest.Value == true then

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