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 4 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)

01local COOLDOWN_TIME = 1
02local cooldown = false
03 
04script.Parent.Touched:connect(function(p)
05     if p.Parent.Name == "Golden Key" then
06        if cooldown then
07            return
08        end
09        cooldown = true
10  script.Parent.Transparency = 0.5
11  script.Parent.CanCollide = false
12  wait(3)
13  script.Parent.Transparency = 0
14          script.Parent.CanCollide = true
15        wait(COOLDOWN_TIME)
16        cooldown = false
17 end
18end)

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:

01local door = game.Workspace.GoldenLock
02local COOLDOWN_TIME = 1
03local cooldown = false
04 
05door.Touched:connect(function(p)
06     if p.Parent.Name == "Golden Key" then
07        if cooldown then
08            return
09        end
10        cooldown = true
11  door.Transparency = 0.5
12  door.CanCollide = false
13  wait(3)
14  door.Transparency = 0
15          door.CanCollide = true
16        wait(COOLDOWN_TIME)
17        cooldown = false
18 end
19end)

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 — 4y
0
@Omenoire (off topic) you joined Roblox in 2008, which is pretty cool. goodlead -62 — 4y
0
@Benbebop Yes it is a localscript inside of StarterCharacterScripts Omenoire 11 — 4y

2 answers

Log in to vote
1
Answered by
Benbebop 1049 Moderation Voter
4 years ago
Edited 4 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

1local RemoteEvent = -- the location of a RemoteEvent
2local UserId = -- the players UserId
3 
4RemoteEvent:FireClient(UserId)

Ref for UserId and FireClient

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

1local RemoteEvent = -- the location of the same RemoteEvent
2 
3RemoteEvent.OnClientEvent:Connect(function()
4    -- door opening code
5end)

Ref for OnClientEvent

Thats really about it, hope this helped!

Ad
Log in to vote
1
Answered by 4 years ago

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

01--Vals--
02--Vals, modify these if needed--
03local KillIntruder = false --Kill if whoever touched it isn't specified User
04local User = "JBennettChief9" --The user who has access
05local OpenTime = 3 --Time the door is open
06local OpenTransparency = 1 --How transparent the door is when it is open
07 
08--Module, do NOT tamper with this--
09script.Parent.Touched:Connect(function(hit)
10    local humanoid = hit.Parent:FindFirstChild("Humanoid")
11    if humanoid then
12        if hit.Parent.Name == User then
13            script.Parent.CanCollide = false
14            script.Parent.Transparency = OpenTransparency
15            wait(OpenTime)
View all 24 lines...

Hope this helped!

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

Answer this question