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

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

Asked by 7 years ago

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

0
If this is a normal script, it cannot access game.Players.LocalPlayer. You should use game.Players.GetPlayerFromCharacter() instead. IDidMakeThat 1135 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago

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!
Ad

Answer this question