Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a open/close door script for certain players? [Partially Solved]

Asked by 5 years ago
Edited 5 years ago

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.

0
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 Nicklaus_s 17 — 4y

2 answers

Log in to vote
1
Answered by 5 years ago

You're guaranteeing that the part that touched will belong to a player character. This will error if the part does not belong to the character.

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)
0
Does your SwampDoor get deleted? User#19524 175 — 5y
0
It dissaperes and i can walk through it and update it closes after a while i would say about 15-20 seconds when i published it and played it through roblox but maybe closer to 5 minutes when i play it on studio JakeDaBeAat 13 — 5y
0
actually closer to 30 seconds, i counted it closed at 28 seconds in roblox should i make it close sooner like 0.5 seconds, i need time for someone to get in their car and drive through as well JakeDaBeAat 13 — 5y
0
Does the script get deleted? User#19524 175 — 5y
View all comments (2 more)
0
no, i went to roblox and played it with a the script and i changed it so it ends the after the line CanCollide = false and added an i statement saying i its transparency is 1 and cncollide is false then it waits 5 and then goes back to normal and when i tried it i think it worked but it wasnt transparent. I caould walk through it but it looked normal and then after 5 seconds i couldnt walk through JakeDaBeAat 13 — 5y
0
i think the wait i whats messed up for everything i try to do i have yet to have a scriot with wait() work. (Referencing my other post and this) JakeDaBeAat 13 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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)

Answer this question