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

How to change the player's team using script?

Asked by 5 years ago

So, I have been trying to make it work, but it didn't work. This is my code below. Basically, I have a part when the player touches the part. The script inside the part where they change the player's team. It didn't work.

local part = script.Parent
part.Touched:Connect(function(hit)
    if hit.Parent then
        local human = hit.Parent:FindFirstChild("Humanoid")
        if human then
            print("Changed Player's Team")
            SetTeam(human,"LevelTwo")
        end
    end
end)

function SetTeam(player, teamName)
    player.TeamColor = game.Teams[teamName].TeamColor
    if player.Character then
        player.Character:BreakJoints()
    end
end

1 answer

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

Well, a player object has a property called team, which can be assigned instead of assigning a team color . One thing you did wrong in your code is that you tried to set the team of the Player's humanoid to that team, not the actual player itself.

With that said

Here is the fixed code(revamped version)

local part = script.Parent
part.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
        print("Changed Player's Team")
        player.Team = game.Teams.LevelTwo
        hit.Parent:BreakJoints()
    end
end)

In this answer, I used the GetPlayerFromCharcter function of the players service, which returns a player when given a character, or it returns nil if the character parameter isn't a character.

game.Players:GetPlayerFromCharacter(instance Character)


References


Note that the Team property does not replicate across the client-server boundary, therefore it cannot be used in local scripts

0
There is an error with IF Statement or at line: 5 GabrielDeaf 4 — 5y
0
yea, i sorted that out theking48989987 2147 — 5y
0
sorry about that theking48989987 2147 — 5y
Ad

Answer this question