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.
01 | tool = script.Parent |
02 | Arrest = script.Parent.Arrest |
03 |
04 | function touch(hit) |
05 | h = hit.Parent |
06 | g = h:findFirstChild( "Humanoid" ) |
07 | if Arrest.Value = = false then |
08 | Arrest.Value = true |
09 | if (g ~ = nil ) then |
10 | h:MoveTo(game.workspace.Jail.Position) |
11 | wait( 10 ) |
12 | Arrest.Value = false |
13 | end |
14 | end |
15 | end |
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.
1 | tool.Unequipped:connect( |
2 | function () |
3 | Arrest.Value = false |
4 | end |
5 | ) |
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.