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

My part color-changing script works in Studio play mode, but not in-game?

Asked by 7 years ago

The script is:

script.Parent.Touched:connect(function(hit)
    green = game.Teams.Green
    purple = game.Teams.Purple
    if game.Players.LocalPlayer.Team == green then
     script.Parent.BrickColor = BrickColor.new(0, 255, 0)
    elseif game.Players.LocalPlayer.Team == purple then
        script.Parent.BrickColor = BrickColor.new(170, 0, 170)
    end
end)

If a player on the green team touches the part, it turns the part green, and if a player on the purple team touches the part, it turns the part purple. This works perfectly in studio, but in-game it doesn't. Please help!

0
Is this a script or localscript? game.Players.LocalPlayer can only be used in local scripts. aztec_glory 63 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

I see two problems with this, but one causing the issue.

  1. connect is deprecated. Please use Connect instead

  2. This appears to be in a part. If this is a LocalScript, it needs to be changed to a Script, because LocalScripts do not work in parts. Additionally, LocalPlayer cannot be used in Scripts.

Use this code, it will fix your problem:

script.Parent.Touched:Connect(function(hit)
    local player=game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        green = game.Teams.Green
        purple = game.Teams.Purple
        if player.Team == green then
            script.Parent.BrickColor = BrickColor.new(0, 255, 0)
        elseif player.Team == purple then
            script.Parent.BrickColor = BrickColor.new(170, 0, 170)
        end
    end
end)

Ad

Answer this question