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

Why change colour ontouch script don't work?

Asked by 8 years ago

Hello everyone, It should change brick color on team color, but probably something is wrong.

Here is script:

local ting = 0

function OnTouch (CLight)
        if ting == 0 then
        ting = 1
        script.Parent.Parent.Parent.Lights.Ball.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C1.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C2.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C3.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C4.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C5.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C6.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C7.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C8.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C9.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C10.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C11.BrickColor = game.Players.LocalPlayer.TeamColor
        script.Parent.Parent.Parent.Lights.C12.BrickColor = game.Players.LocalPlayer.TeamColor
        end
            ting = 0
    end
script.Parent.Parent.CLight.Touched:connect(OnTouch)
0
I think you did 2 things wrong: 1. You have server script (LocalPlayer is nil in server scripts). 2. Your script in Workspace (LocalScripts doesn't work in Workspace) Mokiros 135 — 8y
0
It's not in Workspace. It's in CLight part. DevKarolus1 70 — 8y
0
When people say something is in the Workspace, they mean that it is a descendant of Workspace. (CLight part is in the workspace, or its parent is, or its parent parent is, or its parent parent parent is, etc) Validark 1580 — 8y

1 answer

Log in to vote
0
Answered by
Muoshuu 580 Moderation Voter
8 years ago

First off, I recommend you learn the 'for' statement, as it makes functions such as these much easier

Second, what does 'ting' do? I am assuming it is a debounce (Though it does not work correctly).

Thirdly, is this a 'Script' or 'LocalScript'? As LocalPlayer returns nil in 'Script' objects.


Test this code out to see if I have corrected your issue:


local DebounceTime = 0.5

local Debounce = false
local Lights = script.Parent.Parent.Parent.Lights

 -- Touch events return the part that touched the object they were called from
local OnTouch = function(Part)
    local Humanoid = Part.Parent:FindFirstChild("Humanoid") -- Checks to see whether the parent is a character
    if Humanoid then
        local Player = game.Players:GetPlayerFromCharacter(Humanoid.Parent) -- Tries to get the player that touched it
        if Player then
            if not Debounce then -- Makes sure a touched event hasn't already fired before the time you want it to
                Debounce = true

                local Color = Player.TeamColor

                Lights.Ball.BrickColor = Color

                for I = 1, 12 do -- Sets the color of each light to the player's team color
                    local Light = Lights:FindFirstChild("C"..I)
                    if Light then
                        Light.BrickColor = Color
                    end
                end

                wait(DebounceTime)

                Debounce = false -- Allows the event to fire again
            end
        end
    end
end

script.Parent.Parent.CLight.Touched:connect(OnTouch)
0
Where i should put this script? To workspace or to CLight part? Becouse if it's in CLight part don't work. DevKarolus1 70 — 8y
0
Thanks i created own script, but your helped me too :) DevKarolus1 70 — 8y
Ad

Answer this question