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

How do I make my door only open for one player while using Touched?

Asked by 3 years ago

I'm at a very beginner level of coding and basically want to implement a system where there are 4 keys to open 4 locks before allowing you to pass through a door. So that every player has to find all of the keys, I want a system where the lock will only be opened by an individual player.

Here is my current script:

GoldenLock (the door/lock that will disappear)

local COOLDOWN_TIME = 1
local cooldown = false

script.Parent.Touched:connect(function(p)
     if p.Parent.Name == "Golden Key" then
        if cooldown then
            return
        end
        cooldown = true
  script.Parent.Transparency = 0.5
  script.Parent.CanCollide = false
  wait(3)
  script.Parent.Transparency = 0
          script.Parent.CanCollide = true
        wait(COOLDOWN_TIME)
        cooldown = false
 end
end)

I have tried tinkering with the script and placing it in StarterCharacterScripts, but it still opens for all clients when the key part touches the door:

local door = game.Workspace.GoldenLock
local COOLDOWN_TIME = 1
local cooldown = false

door.Touched:connect(function(p)
     if p.Parent.Name == "Golden Key" then
        if cooldown then
            return
        end
        cooldown = true
  door.Transparency = 0.5
  door.CanCollide = false
  wait(3)
  door.Transparency = 0
          door.CanCollide = true
        wait(COOLDOWN_TIME)
        cooldown = false
 end
end)

I tried adding local to line 5, but either am doing it wrong or simply don't understand the issue. I only learned how to script about a week ago, and would appreciate any help somebody could give.

0
This is a LocalScript correct? Benbebop 1049 — 3y
0
@Omenoire (off topic) you joined Roblox in 2008, which is pretty cool. goodlead -62 — 3y
0
@Benbebop Yes it is a localscript inside of StarterCharacterScripts Omenoire 11 — 3y

2 answers

Log in to vote
1
Answered by
Benbebop 1049 Moderation Voter
3 years ago
Edited 3 years ago

First off

local is simply a constructor for variables, specifically ones that exist only in a certain code block, it has nothing to do with server and client interactions.

The issue

The only reason I could see this happening is because you are using a ServerScript which always interacts with the server no matter where its placed. Or possibly some other problem, but even then it could still be covered by the rest of this.

To get your desired effect you will need to use LocalScripts. There are two methods I can think of: completely client side or both server and client side. Client side will be easier but server and client side will teach you more, id go with the server and client option.

Client side

This one is quite simple, just move all your code into a LocalScript and remove anything non-replicated (Only accessible server side). Make sure that the LocalScript is in a place it will work, certain places completely disable descendant LocalScripts. The places where they will work are the Player's Backpack, Character, PlayerGui, PlayerScripts as well as ReplicatedFirst and ReplicatedStorage.

To clarify the workspace can be accessed by LocalScripts and modified, LocalScripts just can't be placed in it.

Once this is done the door should only open to the client.

Server & Client Side

This method requires RemoteFunctions and/or RemoteEvents.

Have whatever code you need for counting the keys in a ServerScript. Instead of opening it from that script, call a RemoteEvent or RemoteFunction (I will use events for this example) like so:

ServerScript

local RemoteEvent = -- the location of a RemoteEvent
local UserId = -- the players UserId

RemoteEvent:FireClient(UserId)

Ref for UserId and FireClient

Then have a LocalScript ready to receive that event and open the door.

local RemoteEvent = -- the location of the same RemoteEvent

RemoteEvent.OnClientEvent:Connect(function()
    -- door opening code
end)

Ref for OnClientEvent

Thats really about it, hope this helped!

Ad
Log in to vote
1
Answered by 3 years ago

Use a Server Script and place it inside the door. Use this:

--Vals--
--Vals, modify these if needed--
local KillIntruder = false --Kill if whoever touched it isn't specified User
local User = "JBennettChief9" --The user who has access
local OpenTime = 3 --Time the door is open
local OpenTransparency = 1 --How transparent the door is when it is open

--Module, do NOT tamper with this--
script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        if hit.Parent.Name == User then
            script.Parent.CanCollide = false
            script.Parent.Transparency = OpenTransparency
            wait(OpenTime)
            script.Parent.CanCollide = true
            script.Parent.Transparency = 0
        else
            if KillIntruder == true then
                humanoid.Health = 0
            end
        end
    end
end)

Hope this helped!

0
This doesnt seem like your script, are you sure it is yours? Benbebop 1049 — 3y

Answer this question