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.
script.Parent.Touched:Connect(function(part) -- :connect is deprecated, switch to :Connect() local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent) -- Get the player like this if plr then -- MAKE SURE PLAYER EXISTS if plr.Backpack:FindFirstChild("SwampKey") then game.Workspace.SwampDoor.Transparency = 1 game.Workspace.SwampDoor.CanCollide = false wait(5) game.Workspace.SwampDoor.Transparency = 0 game.Workspace.SwampDoor.CanCollide = true end end end)
So I found a weird solution. I used the script below which works and I'm sure has un-necessary things but I'm afraid if I mess with it, it wont work. So I use this script in the detector which makes the door CanCollide = false and then after 3 seconds it CanCollide = True and then I turned both the Transparencies to 1 and went to the door properties and selected Transparency 1 so that the door is always an invisible barrier and if you have the key you can enter. I could never fix the door to become transparent and the not transparent though.
script.Parent.Touched:Connect(function(part) local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent) if plr then if plr.Backpack:FindFirstChild("SwampKey") then game.Workspace.SwampDoor.Transparency = 1 game.Workspace.SwampDoor.CanCollide = false end if game.Workspace.SwampDoor.Transparency == 1 and -- detects if the door is open closes game.Workspace.SwampDoor.CanCollide == false then wait(3) game.Workspace.SwampDoor.CanCollide = true wait(3) game.Workspace.SwampDoor.Transparency = 1 end end end)