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

attempt to get length of local 'player' (a userdata value) Error fix?

Asked by 4 years ago
Edited 4 years ago
function Start()

    local Teams = game:GetService("Teams")

    local Point1 = game.Workspace.Point1

    local teamAlpha = Teams.Alpha
    local teamSpectator = Teams.Spectator

    for _, player in pairs(game.Players:GetChildren()) do
        if player.Team == teamSpectator then
            local Player1 = (player[math.random(1, #player)])
            Player1.Team = teamAlpha
            Player1.TeamColor = teamAlpha.TeamColor
            Player1.CharacterAdded:Connect(function(character)
                character.GetPropertyChangedSignal("Parent"):Wait()
                wait()
                character.HumanoidRootPart.CFrame = Point1.CFrame
            end)
            print(Player1.. "joined the round")
        end

Start()

I know problem in #player but I don't know how to fix it. I need to pick random player and if he in team "Spectator" he gonna to join teamAlpha. But when I try to pick random player error pops-out

RoundScript:12: attempt to get length of local 'player' (a userdata value)

0
You're getting the number of player. You want to get the number of players in the session. Change line 12 to this. local player1 = game.Players[math.random(1,#game.Players:GetPlayers())] killerbrenden 1537 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
function Start()

    local Teams = game:GetService("Teams")

    local Point1 = game.Workspace.Point1

    local teamAlpha = Teams.Alpha
    local teamSpectator = Teams.Spectator
        if player.Team == teamSpectator then
local Player1 = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
            Player1.Team = teamAlpha
            Player1.TeamColor = teamAlpha.TeamColor
            Player1.CharacterAdded:Connect(function(character)
                character.GetPropertyChangedSignal("Parent"):Wait()
                wait()
                character.HumanoidRootPart.CFrame = Point1.CFrame
            end)
            print(Player1.. "joined the round")

Start()
Ad

Answer this question