So I am trying to have a map with certain parts you can only get through once you pay for it. At the moment I have a door called SwampDoor, a normal part called Swamp key with a script to put it in the backpack when touched and a detector, a part flat on ground by the door that when the player with the SwampKey in their back pack steps on it open the SwampDoor waits 5 seconds and closes it. What happens is the player can get the key, they step on the pad and it opens fine but never closes, which is an issue because players who haven't paid for the key can get through. The script for picking up the key works but here's the broken script for the detector.
script.Parent.Touched:connect(function(hit)
local player = hit.Parent.Name
local backpack = game.Players[player].Backpack
if backpack:FindFirstChild("SwampKey") then
workspace.SwampDoor.Transparency = 1
workspace.SwampDoor.CanCollide = false
wait(5)
workspace.SwampDoor.Transparency = 0
workspace.SwampDoor.CanCollide = true
end
end)
And here's the error when I use play script. wait(5):1: attempt to index global 'script' (a nil value)
I'm open to any idea that lets the player with the key through whether its a local script that makes the door always open but just for the specific player who had the key or another script that closes the door if it detects the door is open (I tried it but it didnt work) any help is greatly appreciated.

1 comment
I also recommend to read this as it is what helped me. | https://devforum.roblox.com/t/how-do-i-make-a-door-open-only-for-one-person-solved/494629