Help with Touched and TouchedEnded event

Hello!!! I am completely new to writing Topic. Please let me know if I’m doing anything wrong… I’m also new to scripting.

  1. What do you want to achieve?

I want to make all seat transparency change to 0 when local client touched a seat and all seat transparency to 1 when player stands…

  1. What is the issue?

It causes error

  14:57:24.824  Workspace.1_2cx.Animations:7: attempt to index nil with 'Connect'  -  Client - Animations:7
  14:57:24.824  Stack Begin  -  Studio
  14:57:24.824  Script 'Workspace.1_2cx.Animations', Line 7  -  Studio - Animations:7
  14:57:24.825  Stack End  -  Studio

This is my script I wrote. It’s LocalScript. I don’t know I wrote correct codes, I inserted in StarterCharacterScripts… and Animatons is folder that include seats

local Anims = workspace.Animations
local Seats = Anims:GetChildren()
local Plr = game.Players.LocalPlayer
local player = game:GetService("Players")

for i, child in ipairs(Seats) do
	Seats.Touched:Connect(function()
		if Plr:FindFirstChildWhichIsA("Humanoid")then
		Seats.Transparency = 1
	end
end)

Seats.TouchEnded:Connect(function(hit)
		if Plr:FindFirstChildWhichIsA("Humanoid")then
		Seats.Transparency = 0
	end
end)		
	end
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yes. I have checked DevHub multiple times, and have also checked other websites too. I have tried everything I can.

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You’re trying to use Touched on a table, that’s not how it works as tables by default don’t have a Touched function, use the child thing you specifed in the in pairs loop as the thing use Touched and TouchEnded on

local Anims = workspace.Animations
local Seats = Anims:GetChildren()
local Plr = game.Players.LocalPlayer
local player = game:GetService("Players")

for i, child in ipairs(Seats) do
	child.Touched:Connect(function()
		if Plr:FindFirstChildWhichIsA("Humanoid")then
			child.Transparency = 1
		end
	end)

	child.TouchEnded:Connect(function(hit)
		if Plr:FindFirstChildWhichIsA("Humanoid")then
			child.Transparency = 0
		end
	end)		
end

Also, this is going to work but it’ll work for everyone even if one person touches it, meaning one person touches it, everyone’s seat will change, You need a bit of checks for client sided touches

I recommend reading this guide to help you do that since you’re doing touches locally

1 Like

Thank you so much!!! you are the best!!!

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like