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

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

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!

1 comment

0

Is this a script or localscript? game.Players.LocalPlayer can only be used in local scripts.

1 answer

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)

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