How do you create a when touched system when a player touches a part?

Hello, I’m a builder and I need some support with scripting for a touching system. Basically, I’m attempting a system in which when the player touches a seat, it will sit the player down and anchor them. I’m not sure how to create a touching system, however, and I’m stuck here with this code and I’m unsure how to complete the script from here.
Here’s the code I have:

local player = game.Players.LocalPlayer
local seat = script.Parent

if seat.Touched then
	
end

nonono
do

seat.Touched:Connect(function()
--- stuff here
end)
1 Like

Ohhhhh, sorry I am beginning scripting, thanks for the help.

if it worked can you mark it as the solution? So if someone has the same issue they’d know how to fix it

By default, a seat will cause a character to sit in it when touched. To fix this script, though:

You won’t need to find the local player since this script isn’t operating exclusively for one player.

local seat = script.Parent

seat.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")

	if humanoid then --Check if the part "hit" is connected to a character by determining whether its parent has a Humanoid object
		seat:Sit(humanoid) --Cause the character to sit in the seat
	end
end)

I hope your scripting journey goes well!

I noticed a few issues with this script. First off, you’re trying to reference “LocalPlayer”. I am assuming that this is a local script. If I am correct and this seat instance is in the workspace, then this script will NOT work. The reason behind this is because local scripts will only run if the script is a child or descendant of the following: The player’s character model, the player’s PlayerGui, the player’s backpack, the PlayerScripts folder, or a tool.

1 Like

This is not regarding the issue just a helpful tip
dont code touched event on a local script and .LocalPlayer can only be found on a local script its extremly important that as a new programmer you understand when to use local Scripts and server scripts so read up on them as much as possible: