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

I dont get the "attempt to get length of local 'playing' (a userdata value)" error. can you help?

Asked by 6 years ago

code:

local teams = game:GetService("Teams"):GetTeams()
local teams = game:GetService("Teams"):GetTeams()

local function touched (hit)
    local playing = game.Teams.Playing
    print(#playing)
    if #playing:GetPlayers() == 0 then
        for _, player in pairs(game.Players:GetPlayers()) do
            player.TeamColor = BrickColor.new('Navy blue')
            player.Character.Head:Destroy()
            print('k')
        end
    end
end

script.Parent.Touched:Connect(touched)

error:

Workspace.s.Script:6: attempt to get length of local 'playing' (a userdata value)

the purpose for this script is when you hit a brick then it will restart the match if there are no players on the playing team.

1
game.Teams.Playing doesnt return a table User#23365 30 — 6y
0
doing #playing:GetPlayers() makes no sense User#23365 30 — 6y
1
^ yes it does wdym DaCrazyDev 444 — 6y
0
i dont think :GetPlayers() is a function of a table User#23365 30 — 6y
1
You just said it yourself, game.Teams.Playing doesn't return a table | So if he does game.Teams.Playing:GetPlayers(), where is the function on the table? | GetPlayers() is a Teams function | Review the API a little more, not to be rude DaCrazyDev 444 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

game.Teams.Playing doesn't return a table. You made an error on line 6, and the solution was right in front of you on line 7. Line 7:

if #playing:GetPlayers() == 0 then 

you just have to use :GetPlayers() to get the amount of players on the Team, and then use # to get the length of the table. Corrected:

local teams = game:GetService("Teams"):GetTeams()
local teams = game:GetService("Teams"):GetTeams()

local function touched (hit)
    local playing = game.Teams.Playing
    print(#playing:GetPlayers())
    if #playing:GetPlayers() == 0 then
        for _, player in pairs(game.Players:GetPlayers()) do
            player.TeamColor = BrickColor.new('Navy blue')
            player.Character.Head:Destroy()
            print('k')
        end
    end
end

script.Parent.Touched:Connect(touched)

You obviously can't get the length of that userdata value, but you can get the length of a table, so use :GetPlayers() which returns a table of all of the players. That was sorta like a typo.

If that worked, plaese accept my answer \ if it didn't, leave a comment and let me know or if you're confused on something

2
thanks! I really appreciate it! SodaZere 31 — 6y
1
No problem :) DaCrazyDev 444 — 6y
Ad
Log in to vote
1
Answered by 6 years ago

The '#' operator is used to get the index count of an array. By the looks of it, 'Playing' is a team instance, which does not define any method to handle a length operator.

I think your intent was to check the length of the array returned by Team:GetPlayers()

Answer this question