script.Parent.Touched:Connect(function(hit) if hit.Parent:GetChildren("Humanoid") then game.Lighting.ClockTime = 18 game.Lighting.OutdoorAmbient = Color3.fromRGB(127,127,127) game.Lighting.ColorShift_Top = Color3.fromRGB(0,0,0) end end)
that's the code
Replace if hit.Parent:GetChildren("Humanoid") then
with if hit.Parent:FindFirstChild("Humanoid") then
. For a more generalized and complete solution, you can use this function:
function getCharacterFromDescendant(instance) local character = instance while not character or not character:FindFirstChild("Humanoid") do character = character:FindFirstAncestorWhichIsA("Model") end return character end
The reason the above function is suggested is due to the fact that the part that touched may not be a direct child of the potential character (namely accessories).
The problem was that you're using GetChildren()
when you should be using FindFirstChild(name)
instead. GetChildren()
returns a list of the instance's children whereas FindFirstChild(name)
returns the child that is named name if there is one. There is also a method called WaitForChild(name)
which waits for a child named name to get added to the said instance. Don't get confused between these methods!
Look in output for errors, if there are none it's either working or not running, you can print something on line 1 to see if it is. If it's not running it's either disabled or misplaced. Scripts will only run when placed in Workspace or ServerScriptService. If it's a localscript you can check the wiki for places where it needs to be to run.
The problem isn't caused by an update. You don't want to use GetChildren()
. GetChildren()
returns an array of the children of the instance you use it on. You're going to want to use FindFirstChild()
as if the instance is found, it will run the rest of the code. Otherwise, if the instance isn't found, the code will not run. Here is the code you should be using:
script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Lighting.ClockTime = 18 game.Lighting.OutdoorAmbient = Color3.fromRGB(127, 127, 127) game.Lighting.ColorShift_Top = Color3.fromRGB(0, 0, 0) end end
That script wouldn't do what you want all. The reason is because the Instance.GetChildren
method returns an array and takes no arguments, so it won't look for a Humanoid
inside of hit.Parent
. It would just always execute because a table is a truthy value. We can accomplish this by using the Instance.FindFirstChild
method.
script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then -- If no humanoid, the code below won't run! game.Lighting.ClockTime = 18 game.Lighting.OutdoorAmbient = Color3.fromRGB(127,127,127) game.Lighting.ColorShift_Top = Color3.fromRGB(0,0,0) end end)