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

Code door in localscript for avoid other player across the door ?

Asked by 5 years ago

Hi, i have a Door Code script that works, i just need function when players enter the correct code, the door stay open only for them.

The door work perfectly, i just need a localscript for avoid player doesn't have code to cross the door in the same time of player have code ^^

The code:

Code = "9796" --change To any numbers You like
Input = ""


--This Is for my buddy siros

--created by
------------------------------------------
--Clear And Enter

function Clear()
print("Cleared")
Input = ""
end

script.Parent.Clear.ClickDetector.MouseClick:connect(Clear)




function Enter()
if Input == Code then
print("Entered")
Input = ""

local door = script.Parent.Parent.Door

door.CanCollide = false
wait(3)
door.CanCollide = true



return end
Input = ""
print("Wrong Code")
end




script.Parent.Enter.ClickDetector.MouseClick:connect(Enter)

under this script i have this function for button

function Click0()
Input = Input..0
print("0") 
script.Parent.B0.Decal.Texture = "http://www.roblox.com/asset/?id=2767674"
wait(0.1)
script.Parent.B0.Decal.Texture = "http://www.roblox.com/asset/?id=2761903"
end

script.Parent.B0.ClickDetector.MouseClick:connect(Click0)

I have this for B0,B1,B2 etc.. for number 1 to 9

0
So you want the door to open only for the person that entered the code and not anyone else? Vezious 310 — 5y
0
You should not use deprecated code like :connect. Also please indent your code properly when programming it makes it easier to read and understand as well as debug. User#21908 42 — 5y
0
Yes Vezious Thorngard 4 — 5y
0
You can just put the local script in starterplayerscripts. You can put the serverscript wherever you want it as long as you have a variable for the door and the player that entered the code. You can even use the server sided script in the script you already have. User#21908 42 — 5y
View all comments (2 more)
0
it is all up to you. User#21908 42 — 5y
0
For the love of god use local variables. Usage of global variables is a bad practice because they make code messy, and in conjunction with poorly indented code even messier. Global variables also pollute the global namespace, and can increase risks for name collisions. It's best to avoid any conflict. User#19524 175 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

What you can do is fire a remote event that the client listens for and when that remote event is fired open the door locally so that nobody else has access. The trick is to use a LocalScript and open the door from there. Here is an example from my past work:

-- ServerScript --
local player = players:FindFirstChild(playerName)

if player then

    doorOpener:FireClient(player, script.Parent) -- for me script.Parent was the door model

end

And LocalScript:

-- This script is by Phlegethon5778 --

-- Variables --

local replicatedStorage = game:GetService("ReplicatedStorage")
local doorOpener = replicatedStorage:WaitForChild("OpenDoorEvent")

-- Event --

if doorOpener then

    doorOpener.OnClientEvent:Connect(function(door)


        local doorPartList = door:GetChildren()

        if #doorPartList > 0 then

            for i,v in pairs(doorPartList) do

                if v:IsA("BasePart") then

                    v.CanCollide = false
                    v.Transparency = 1

                end

            end


        end

    end)

end

For this script to work I had to have a remote event instance in ReplicatedStorage named "OpenDoorEvent". I fired the event from the server on an event. In your case, when they enter the correct code. Then when that is accomplished just put the event listener in the client and make the function that runs when the event is fired open the door. I hope this helps and have a great day scripting!

Extra: More on remote events here, more on events here, and more on server/client relationship here.

0
Thanks, but were i can place this script ? i have create a remote event in ReplicatedStorage, but were i place the localscript and the script ? Thorngard 4 — 5y
Ad

Answer this question