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

Using a key tool to open a door for one client only?

Asked by 4 years ago

Hello, I posted about this yesterday and got some assistance but I am once again stuck with my script. I'm more confused than before but I think I am closer to the solution than I was before.

Here is what I want to do:

Player picks up a key from the ground which gives them a tool called "Golden Key" (done) Player touches their key tool to the door which is a part called "GoldenLock" - which then opens the door for only them. (Problems occur here)

Overall, I want 4 tool keys that a player physically holds up to 4 doors layered onto each other, which I plan on unioning individually (as in, 1 door for 1 key) and making them like "chains" for the real door behind them, so as each one disappears, one lock for the door will go away until finally they are allowed to pass through. I would likely layer 4 "locks" on top of each other and using the scripts below, just individually have them become cancollide = false and transparency = 1.

Hopefully this explanation makes sense, I may be rambling a little bit but I just wanted to fully explain my objective here.

Here are my scripts, they are likely very wrong as I am uncertain of how to do a lot of things with remote events...

Local Script, located in StarterPlayer.StarterCharacterScripts

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

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

RemoteEvent.OnClientEvent:Connect(function()
    local function onTouch(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
end)

Server Script, located in ServerScriptService

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

RemoteEvent.OnServerEvent:Connect(game.Player.userId)

I kept on tinkering with the scripts and every time I thought I figured out the solution, I kept on getting errors and continued to change things until I just kind of came to this mess. I don't even know if this is right, but I think I may be somewhat on the right track.. I don't know if I need to get a unique user for each player that contacts the door, but the way to activate the door is through the key the player is holding. Is this even possible to do for only one client, or is it doomed because the key would technically be server side? Or is it possible because it's a part of the player's individual backpack?

Any assistance I could receive would help immensely, I only completed some scripting lessons from Studio+ about a week ago and had some issues retaining all of the info. I don't want to find a workaround for this so that I may learn from this and become better at scripting. If you have read all of this, thank you very much.

0
Woah you joined 2008. iivSnooxy 248 — 4y
0
@ivvSnooxy - Yes, I came back after being gone since about 2012 to learn programming and try to make some high quality games if possible. Omenoire 11 — 4y

1 answer

Log in to vote
0
Answered by
016er 9
4 years ago

One problem I immediately noticed was that you're not firing the server at all, and even so it's the client you need to fire at, since you're going to open that door only for the one client.

In this case, we'd have to use :FireClient(). I assume the key touches the door in order for it to open, so we'll use a touch event with a debounce so that it doesn't fire your event 100 times.

The script below would likely have to be inside a door.

local db = false

script.Parent.Touched:Connect(function(hit)
    if db == false then
        db = true
        local player = game.Players:FindFirstChild(hit.Parent.Name)
        game.ReplicatedStorage.RemoteEvent:FireClient(player)
        wait(1)
    end
    db = false
end)

On your client-side (localscript), you also have to input WHICH client it's directed at, hence why we defined and added "player" as an argument to :FireClient()

RemoteEvent.OnClientEvent:Connect(function()

this line, would likely be replaced with the line below.

RemoteEvent.OnClientEvent:Connect(function(player)

If there are any other issues, let me know.

0
On line 7 I get this error: "FireClient: player argument must be a Player object" - the server script is inside of the door now and the localscript is still where it was before with the update you sent Omenoire 11 — 4y
0
That's very odd, it should get you the player itself. Are you sure you pasted it correctly? 016er 9 — 4y
0
You do not need to add the player as an argument when using OnClientEvent, since the local script already know who the client is. DeUltimate23 142 — 4y
0
I just double-checked it and it still errors in that same way. Is there possibly an issue in my Local script? I'm not quite sure what to do from here. Just to clarify, the door is supposed to open when the "Golden Key" tool touches it. Omenoire 11 — 4y
Ad

Answer this question