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()
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.
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
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.
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.