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

How to make pointlights change color with a function?

So I made a script that while touched, turns the color of all the lights in a game red, and when not touched they turn back to white. This script works perfectly on it's own currently. I would like to make it so that the point lights inside the lights turn red while touched as well, and back to white when not touched. The problem is, the script i made including this doesn't work. I did manage to make a script that does turn the point lights red, but the lights and point lights dont turn back to white after the player stops touching it.

Script with PointLights

local barrier = script.Parent
for index,part in pairs(game.Workspace.lights:GetChildren()) do
        if part:IsA("Part") then do   
        barrier.Touched:connect(function()
        part.BrickColor = BrickColor.new("Bright red")
        part.PointLight.Color = Color3.new(1, 0, 0)     
        end)
        barrier.TouchEnded:connect(function() 
        part.BrickColor = BrickColor.new("Institutional white")
        part.PointLight.Color = Color3.new(1, 1, 1) 
        end)      
    end
end
end         

Script without point lights:

local barrier = script.Parent
for index,part in pairs(game.Workspace.lights:GetChildren()) do
        if part:IsA("Part") then do   
        barrier.Touched:connect(function()
        part.BrickColor = BrickColor.new("Bright red")
        end)
        barrier.TouchEnded:connect(function() 
        part.BrickColor = BrickColor.new("Institutional white")
        end)      
    end
end
end         

script that makes the point lights and lights turn red, but not back to white when not touched

local barrier = script.Parent
barrier.Touched:connect(function()
    for i, v in pairs(lights:GetChildren()) do
        v.BrickColor = BrickColor.new("Bright red")
        for index, value in pairs(v:GetChildren()) do
            value.Color = Color3.new(1,0.109804, .109804)
        end
    end
end)
barrier.TouchedEnded:connect(function()
    for i, v in pairs(lights:GetChildren()) do
        v.BrickColor = BrickColor.new("Institutional white")
        for index, value in pairs(v:GetChildren()) do
            value.Color = Color3.new(1,1,1)
        end
    end
end)

1 comment

0

TouchEnded isn't reliable

1 answer

Touched and touchended fires multiple times, since a character exists out of multiple parts and for each part it fires touched and touchended and also because the character is animated which causes it touched and touchended to fire even more, you should use a debounce which prevents touched from firing multiple times

see this article on debounce

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