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

How can I edit my script to allow the game to function in online mode?

I'm working on a game that allows blocks you touch to change colour based on your team colour, I was wondering what was wrong with this script that did not allow it to work in online mode. In test mode in studio it works perfectly. This is in a normal script in the part;

function onTouch(hit)
    local player = game.Players.LocalPlayer
    if player.TeamColor == BrickColor.new("Really red") then
		script.Parent.BrickColor = BrickColor.new("Really red")
	elseif player.TeamColor == BrickColor.new("New Yeller") then
		script.Parent.BrickColor = BrickColor.new("New Yeller")
	elseif player.TeamColor == BrickColor.new("Lime green") then
		script.Parent.BrickColor = BrickColor.new("Lime green")
	elseif player.TeamColor == BrickColor.new("Toothpaste") then
		script.Parent.BrickColor = BrickColor.new("Toothpaste")
	elseif player.TeamColor == BrickColor.new("Really blue") then
		script.Parent.BrickColor = BrickColor.new("Really blue")
	elseif player.TeamColor == BrickColor.new("Hot pink") then
		script.Parent.BrickColor = BrickColor.new("Hot pink")
	end
end

script.Parent.Touched:connect(onTouch)

I've experienced this problem before with click detectors, and without a solution none of my games are working. Please send help :c

1 comment

0

If this is a normal script, it cannot access game.Players.LocalPlayer. You should use game.Players.GetPlayerFromCharacter() instead.

1 answer

✓ Accepted answer

You can only use LocalPlayer in local scripts.

Because Local Scripts don't work in workspace, we'll use the GetPlayerFromCharacter function of the Players Service.

function onTouch(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)-- Gets player
    if player then-- Makes sure player exists
        if player.TeamColor == BrickColor.new("Really red") then
            script.Parent.BrickColor = BrickColor.new("Really red")
        elseif player.TeamColor == BrickColor.new("New Yeller") then
            script.Parent.BrickColor = BrickColor.new("New Yeller")
        elseif player.TeamColor == BrickColor.new("Lime green") then
            script.Parent.BrickColor = BrickColor.new("Lime green")
        elseif player.TeamColor == BrickColor.new("Toothpaste") then
            script.Parent.BrickColor = BrickColor.new("Toothpaste")
        elseif player.TeamColor == BrickColor.new("Really blue") then
            script.Parent.BrickColor = BrickColor.new("Really blue")
        elseif player.TeamColor == BrickColor.new("Hot pink") then
            script.Parent.BrickColor = BrickColor.new("Hot pink")
        end
    end
end

script.Parent.Touched:connect(onTouch)

Good Luck!

If I helped, please don't forget to accept my answer!

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