Trying to make a door that only lets you pass if you have a certain tool. It works in studio, but wont work in game despite the fact I've had it in a script and a localscript. This is the code.
1 | script.Parent.Touched:connect( function (player) |
2 | local tool = player.Backpack.Ticket |
3 | if tool ~ = nil then |
4 | script.Parent.CanCollide = false |
5 | wait(. 5 ) |
6 | script.Parent.CanCollide = true |
7 | else |
8 | end |
9 | end ) |
the Touched
event does not automatically send the player as a parameter. Instead, it sends the part that touched script.Parent
01 | script.Parent.Touched:connect( function (part) |
02 | local player = game.Players:GetPlayerFromCharacter(part.Parent) --checks if a player's limb touched it, then gets its player. |
03 | if player then |
04 | local tool = player.Backpack.Ticket |
05 | if tool then -- "~= nil" is not necessary |
06 | script.Parent.CanCollide = false |
07 | wait(. 5 ) |
08 | script.Parent.CanCollide = true |
09 | end -- "else" wasn't necessary either. |
10 | end |
11 | end ) |
You needed to define the player by using GetPlayerFromCharacter
:
01 | script.Parent.Touched:connect( function (p) |
02 | local player = game.Players:GetPlayerFromCharacter(p.Parent) --You needed to get the player who touched it. |
03 | local tool = player.Backpack:WaitForChild( "Ticket" ) |
04 | if tool ~ = nil then |
05 | script.Parent.CanCollide = false |
06 | wait(. 5 ) |
07 | script.Parent.CanCollide = true |
08 | else |
09 | end |
10 | end ) |
Please accept my answer if this helped!