Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago
Edited 6 years ago
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

0
holy cow 4 answers?! DuckMaster11211 13 — 6y

4 answers

Log in to vote
1
Answered by
RayCurse 1518 Moderation Voter
6 years ago
Edited 6 years ago

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!

0
I'm the slowest of them all mwahahaha. RayCurse 1518 — 6y
0
If my answer doesn't get accepted I will commit neck rope. lunatic5 409 — 6y
0
My answer is obviously the most correct and valid. RayCurse 1518 — 6y
0
True, but I need more rep bröther. lunatic5 409 — 6y
Ad
Log in to vote
0
Answered by
gullet 471 Moderation Voter
6 years ago

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.

0
the script works in studio DuckMaster11211 13 — 6y
0
Test with a local server and look in the outputs for errors gullet 471 — 6y
Log in to vote
0
Answered by
lunatic5 409 Moderation Voter
6 years ago
Edited 6 years ago

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
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. gullet 471 — 6y
0
My bad. lunatic5 409 — 6y
Log in to vote
0
Answered by 6 years ago

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.
0
lol beat you to it lunatic5 409 — 6y
0
your 2 slo!!!!! lunatic5 409 — 6y
0
slow and steady wins the race RayCurse 1518 — 6y
0
time to delete my answer so it's the last ha User#19524 175 — 6y
0
slow and steady indeed. DuckMaster11211 13 — 6y

Answer this question