Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Scripting updates caused this lighting script to not work in a local server. Help?

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

1 comment

0

holy cow 4 answers?!

4 answers

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!

4 comments

0

I'm the slowest of them all mwahahaha.

0

If my answer doesn't get accepted I will commit neck rope.

0

My answer is obviously the most correct and valid.

0

True, but I need more rep bröther.

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.

2 comments

0

the script works in studio

0

Test with a local server and look in the outputs for errors

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

2 comments

0

It doesn't return a boolean, it returns the child or nil in an if statement they will evaluate as truthy or not. But yes he shouldn't be using GetChildren since that will return an array with all children.

0

My bad.

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)

######Hopefully this answered your question and if it did, don't forget to hit that accept button. If you have any other questions then feel free to leave them down in the comments.

5 comments

0

lol beat you to it

0

your 2 slo!!!!!

0

slow and steady wins the race

0

time to delete my answer so it's the last ha

0

slow and steady indeed.

This discussion is preserved as it was. New answers, comments, and votes are closed.