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

When my tool is unequipped, it won't change back to a false value? How come?

Asked by
Yeevivor4 155
9 years ago

Hello, what I'm trying to do is make an arrest tool that teleports a person to a jail if the tool handle touches him. At first, it kept putting the person in jail a lot of times, so I put a BoolValue. The BoolValue says that if the BoolValue is false, and the tool touches a person, he will get teleported and the BoolValue will become true. After 10 seconds it becomes false. The problem is that when I unequip it, the value won't go back to false anymore, rendering the tool useless.

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
h:MoveTo(game.workspace.Jail.Position)
wait(10)
Arrest.Value = false
end
end
end

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

tool.Equipped:connect(sel)

1 answer

Log in to vote
1
Answered by 9 years ago

Problem: BoolValue Arrest doesn't turn off when tool goes unequipped. Cause: There is nothing in the code to tell the boolean to turn off when the tool is unequipped. You only made it turn off after 10 seconds. Solution: Use the Unequipped event to turn it off.

tool.Unequipped:connect(
    function()
        Arrest.Value = false
    end
)

Other things to note: -Lack of use of local variables to optimize the script (see this: -Instead of using (variable ~= nil) you can use variable standing alone because it still works. Because if it is true then it automatically returns the if statement as true. -Indent your scripts. -Look up "Debounce" on the wiki if you want to use cooldown instead of unequip.

Ad

Answer this question