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

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

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.

5 comments

1

game.Teams.Playing doesnt return a table

0

doing #playing:GetPlayers() makes no sense

1

^ yes it does wdym

0

i dont think :GetPlayers() is a function of a table

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

2 answers

✓ Accepted answer

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 comments

2

thanks! I really appreciate it!

1

No problem :)

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()

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