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.
script.Parent.Touched:connect(function(player) local tool = player.Backpack.Ticket if tool ~= nil then script.Parent.CanCollide = false wait(.5) script.Parent.CanCollide = true else end end)
the Touched
event does not automatically send the player as a parameter. Instead, it sends the part that touched script.Parent
script.Parent.Touched:connect(function(part) local player = game.Players:GetPlayerFromCharacter(part.Parent) --checks if a player's limb touched it, then gets its player. if player then local tool = player.Backpack.Ticket if tool then -- "~= nil" is not necessary script.Parent.CanCollide = false wait(.5) script.Parent.CanCollide = true end -- "else" wasn't necessary either. end end)
You needed to define the player by using GetPlayerFromCharacter
:
script.Parent.Touched:connect(function(p) local player = game.Players:GetPlayerFromCharacter(p.Parent) --You needed to get the player who touched it. local tool = player.Backpack:WaitForChild("Ticket") if tool ~= nil then script.Parent.CanCollide = false wait(.5) script.Parent.CanCollide = true else end end)
Please accept my answer if this helped!