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

Script only getting the first person's to join points?

In my function to find the mvp, the function only gets the points of the first player to join can anyone help me?

function findMVP()
	local allStats = {}
	for _,players in pairs(game.Players:GetChildren()) do
		if players.IsPlayerInRound.Value == true then
			table.insert(allStats,players.Name..' has won with '..players.leaderstats.Points.Value..' points!')
			table.sort(allStats,function(x,y) return x > y end)
			for _,stats in pairs(allStats) do
				print(stats)
			end
			return allStats[1]
		else
			print(players.Name.." isn't in the round!")
		end
	end
end

1 comment

0

Try GetPlayers() instead

1 answer

Consider using this code instead:

function findMVP()
	local bestScore = 0
	local mvp = nil
	for _, player in pairs(game.Players:GetPlayers()) do
		if player.IsPlayerInRound.Value then
			local score = player.leaderstats.Points.Value
			if not mvp or score > bestScore then
				mvp = player
				bestScore = score
			end
		end
	end

	-- `mvp` should now be set to best player (or nil
	--    if no such player exists)
	-- Remaining code logic goes here
end

This code iterates through all the players, keeping track of the best player and their corresponding score at each iteration. From this, we can guarantee that the best player will be found at the end (or will be nil if no player is in the round).

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