Gate open only for you(help)

i want to make a gate that open only for you when you touched the part, but i have problem with the touch part, its just not working. and i also dont know how to connect the scripts, like when you touched the part the door open only for you. thanks

local script:

local part = script.Parent
local rp = game:GetService("ReplicatedStorage")
local remoteEvent = rp:WaitForChild("RemoteEvent")


part.Touched:Connect(function(hit)
	if hit.Parent == game.Players.LocalPlayer.Character then
		print("hello")
	end
end)

door script:

local ts = game:GetService("TweenService")


local door = game.Workspace.Door1
local doorRoot = door.PrimaryPart

local DoorSwingInfo = TweenInfo.new()

local DoorSwingTween = ts:Create(doorRoot, DoorSwingInfo, {
	CFrame = doorRoot.CFrame  * CFrame.Angles(0, math.rad(130), 0)
})

local DoorSwingInfo2 = TweenInfo.new()

local DoorSwingTween2 = ts:Create(doorRoot, DoorSwingInfo2, {
	CFrame = doorRoot.CFrame  * CFrame.Angles(0, math.rad(0), 0)
})

wait(5)
DoorSwingTween:Play()

wait(4)

DoorSwingTween2:Play()

Use a single LocalScript.

local ts = game:GetService("TweenService")


local door = game.Workspace.Door1
local doorRoot = door.PrimaryPart

local DoorSwingInfo = TweenInfo.new()

local DoorSwingTween = ts:Create(doorRoot, DoorSwingInfo, {
	CFrame = doorRoot.CFrame  * CFrame.Angles(0, math.rad(130), 0)
})

local DoorSwingInfo2 = TweenInfo.new()

local DoorSwingTween2 = ts:Create(doorRoot, DoorSwingInfo2, {
	CFrame = doorRoot.CFrame  * CFrame.Angles(0, math.rad(0), 0)
})

wait(5)
DoorSwingTween:Play()

But one of the issues here is that you did not give TweenInfo.new its required parameters. Please inform us of related errors about the code given in the Output.

so how do i do that the gates opens when you touched the part?

Add a .Touched event on the server

in another script? and if yes how do i connect the scripts

If your local script is placed in workspace, that’s the issue. LocalScripts can only run in certain areas:


There’s 2 ways you can handle this:

  • Detect the touch event from the client and run the code, but you’d need to place your local script under an awkward parent
  • Detect the touch event from the server and fire a remote event to the client of the player who touched it

Client Approach:

local part = --[[Directory of the part for the player to touch]]--

part.Touched:Connect(function(hit)
   if not hit:IsDescendantOf(game:GetService("Players").LocalPlayer.Character) then
      -- this checks if the thing that touched the part is a descendant of the player's character
      return -- stops the function if not
   end
   -- code to be executed
end)

Server Approach:

local part = script.Parent -- assuming the server script is a child of the part
local replicatedStorage = game:GetService("ReplicatedStorage") -- whatever service the remote event is a child of
local remote = replicatedStorage:WaitForChild("Remote Event Here")

part.Touched:Connect(function(hit)
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    -- checks if the thing that touched the object is part of a player's character

    if player then -- checks if a player was found
       remote:FireClient(player) -- fires the remote event to the player's client
    end
end)

-- Detect the event on the client using .OnClientEvent in a localscript for the remote event and run the code to open the door

https://developer.roblox.com/en-us/api-reference/class/RemoteEvent
https://developer.roblox.com/en-us/api-reference/event/RemoteEvent/OnClientEvent
https://developer.roblox.com/en-us/api-reference/function/RemoteEvent/FireClient

1 Like

Thank you very much I will try it know

i need help im new to scripting i dont understand so much. can you be more specific.

The Touched event of BasePart instances triggers when another part comes in collision with another part. With this in mind, you could run the wanted code when this happens.

local part;
local Players = game:GetService('Players')
local LocalPlayer = Players.LocalPlayer

local function OnTouch(Part)
    if Players:GetPlayerFromCharacter(Part.Parent) == LocalPlayer then
        -- Identify if the touching part belongs to a player (Ex.: the left arm of the player)

        -- Code
    end
end

part.Touched:Connect(OnTouch)

Make sure to a add debounce in order to prevent the code from being spammed:

If you have any further questions about the API, read the Developer Hub, which you can use as a reference.

1 Like

and where do i put the local script with the part. in workspace?

Please read what zyro_crimson stated. You’ll have to move the LocalScript somewhere else, which means you’ll also need to move the part, but you should not place it under the script, but directly under Workspace instead.

1 Like

i put the local script in players and its not working for me and it does HDAdmin error. pls help me