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

String expected, got object? I'll be putting the explorer tree in the post.

Hello!

First of all, sorry for the brief title, I wasn't really sure what to make it.

Anyway, I'm not really sure to start so here's the error.

23:36:16.918 - Workspace.GameScripts.PlayerLeft:19: bad argument #2 (string expected, got Object)

Here's the script. If the second last player who is either on the "playing" team, or the "complete" team leaves, "v" in players should be awarded a win. This is just a brief explanation of it. I should also add that the wins object is an IntValue.

game.Players.PlayerRemoving:Connect(function(plr)
	local numPlaying = #game.Teams.Playing:GetPlayers()
	local numComplete = #game.Teams.Complete:GetPlayers()
	local numLobby = #game.Teams.Lobby:GetPlayers()
	
	print(numPlaying.."playing")
	print(numComplete.."complete")
	print(numLobby.."lobby")
	print(plr.Name.."plr")
	print("player removed")
	
	if plr.Team ~= game.Teams.Lobby then
		if numComplete == 0 then
			if numPlaying == 1 then
		
				local playing = game.Teams.Playing:GetPlayers()
				
				for i,v in pairs(playing) do
					game.Players[v].leaderstats.Wins.Value = game.Players[v].leaderstats.Wins.Value + 1
				end
			end
		end
	end 
end)

Here's the explorer.

1 answer

✓ Accepted answer

playing's v in the for loop represents as the player class, use v.Name to extract the certain attribute of the player.


game.Players.PlayerRemoving:Connect(function(plr)
    local numPlaying = #game.Teams.Playing:GetPlayers()
    local numComplete = #game.Teams.Complete:GetPlayers()
    local numLobby = #game.Teams.Lobby:GetPlayers()

    print(numPlaying.."playing")
    print(numComplete.."complete")
    print(numLobby.."lobby")
    print(plr.Name.."plr")
    print("player removed")

    if plr.Team ~= game.Teams.Lobby then
        if numComplete == 0 then
            if numPlaying == 1 then

                local playing = game.Teams.Playing:GetPlayers()

                for i,v in pairs(playing) do
                    game.Players[v.Name].leaderstats.Wins.Value = game.Players[v.Name].leaderstats.Wins.Value + 1
                end
            end
        end
    end 
end)

More details: Wiki>Player.

4 comments

0

You can also do game.Players[i].leaderstats.Wins.Value = game.Players[i].leaderstats.Wins.Value + 1 to index the players list with the current loop value it's on.

0

You need it to be a table, so do game.Players:GetChildren().

0

Or game.Players:GetPlayers().

0

Yeah. :))

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