I have a LocalScript inside of StarterGui with a function that triggers whenever a specific part is touched by the player. However, I get an "attempt to index nil with ‘Touched’ " error on the output when running the game.
This has never happened to me before. Are “Touched” functions not functional in LocalScripts? If so, what can I do instead?
This is the script:
local lighting = game:GetService("Lighting")
local players = game:GetService("Players")
local folder = workspace:WaitForChild("ChurchTP")
local door1 = folder:WaitForChild("A")
local door2 = folder:WaitForChild("B")
local interiorambient = Color3.fromRGB(106, 95, 61)
local exteriorambient = Color3.fromRGB(28, 47, 119)
local debounce = true
local function ambient(part)
local player = players.LocalPlayer
if part.Parent == player.Character and debounce == true then
debounce = false
lighting.Ambient = interiorambient
task.wait(0.5)
debounce = true
end
end
door1.Touched:Connect(ambient)
Edit: Previously, I was using this code:
local lighting = game:GetService("Lighting")
local players = game:GetService("Players")
local folder = workspace:WaitForChild("ChurchTP")
local door1 = folder:WaitForChild("A")
local door2 = folder:WaitForChild("B")
local interiorambient = Color3.fromRGB(106, 95, 61)
local exteriorambient = Color3.fromRGB(28, 47, 119)
local debounce = true
door1.Touched:Connect(function(part)
local player = players.LocalPlayer
if part.Parent == player.Character and debounce == true then
debounce = false
lighting.Ambient = interiorambient
task.wait(0.5)
debounce = true
end
end)
but changed it thinking that it could possibly solve the issue. It made no difference; same error.
--
if door1 then
print("exists")
else
print("does not exist")
end
before setting up the Touched event, this will tell you if door1 does not exist by the time you’re trying to set the event.
Also, is there a specific reason you’re choosing StarterGui over something like StarterPlayerScripts? Usually you’d want to put these types of scripts in the latter unless it’s UI related
the error is saying that door1 is nil, are you sure the door still exists when you are setting up the touched event?
It does not say that either of the doors are nil. I was getting this issue before I set the “local door1 = folder:WaitForChild(“A”)” line.
Maybe because a part that touched the door doesn’t have a Parent. Try checking if the part is a parent first ig
It has nothing to do with that.
Try a debug print before setting up the Touched event, this will tell you if door1 does not exist by the time you’re trying to set the event.
It tells me that it exists. I set it to print the door’s name and it works. The problem seems to be entirely on the Touched function.
Also, is there a specific reason you’re choosing StarterGui over something like StarterPlayerScripts? Usually you’d want to put these types of codes in the latter unless it’s UI related
I was looking for a way to pull this off. A client script inside the folder of the doors didn’t seem to work, so I tried StarterGui instead and it worked.
Try putting this script in StarterPlayer → StarterPlayerScripts
local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local folder = workspace.ChurchTP
local door1 = folder:WaitForChild("A")
local door2 = folder:WaitForChild("B")
local interiorAmbient = Color3.fromRGB(106, 95, 61)
local exteriorAmbient = Color3.fromRGB(28, 47, 119)
local debounce = true
door1.Touched:Connect(function(part)
if part.Parent == LocalPlayer.Character and debounce == true then
debounce = false
Lighting.Ambient = interiorAmbient
task.wait(0.5)
debounce = true
end
end)
Just a side note, LocalScripts will only work in certain places. Those places are: StarterPack, StarterGui, StarterPlayerScripts, StarterCharacterScripts, and ReplicatedFirst.
If you want your LocalScripts to work in certain places other than the ones I mentioned for organization or whatever your personal reasons are, you can actually put this code in a regular script and set the RunContext to Client.