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

How do I award points?

I want a script that starts a round and then at the end of a round checks who survived and gives them points. But if there is only one player left before the round time limit is up then it just gives them the points. So what I have to do is insert all the players in the round into a table called "PlrsInGame."

intermission = 25
gametime = 180

while true do
	wait(intermission)
	repeat wait() until #game.Players:GetChildren()>0
	playersingame = {}
	for i,player in pairs(game.Players:GetChildren()) do
	table.insert(playersingame,player)
	end

	for index,value in pairs(playersingame) do
	print(value)
	end
	wait(gametime)
end

Now how do I remove the player from the table when he dies. Then at the end of the round how do I give points to the survivors, or if there is only one survivor before the end of the round how do I give him points? I have a leaderboard and the "points" are "Gold." Please help me with this? (if it makes any sense.)

1 answer

You have a good start by making the table, but instead of using the array part, I suggest making a dictionary. This will make it easier to see which players survived and which didn't.

To add players to the table, do this:

--the '_' variable indicates that we won't be using the variable in the following code block. This is just a convention that is used to make code easier to read
for _, player in pairs(game.Players:GetChildren()) do
	playersingame[player] = true
end

This will set the starting point, where every player gets assigned to a value of true, aka. they are alive.

When a player dies, all you have to do is this:

playersingame[playerThatDied] = false

Now, at the end of the round, when you need to check for survivors, you can loop through the dictionary with a for loop and see if their value is true.

for player, survived in pairs(playersingame) do
	if survived then
		-- add points to player
	end
end

Now that you know which players survived, you need to update their leaderboard.

player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + 1

Here is the full code:

intermission = 25
gametime = 180

while true do
	wait(intermission)
	repeat wait() until #game.Players:GetChildren()>0
	
	playersingame = {}
	for _, player in pairs(game.Players:GetChildren()) do
		playersingame[player] = true
		-- If you haven't already bound an event when player dies, here it is
		player.Humanoid.Died:connect(function()
			playersingame[player] = false
		end)
	end

	wait(gametime)
	
	-- I assume this is when you check for players that survived
	for player, survived in pairs(playersingame) do
		if survived then
			player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + 1 -- Replace 1 with the amount of gold a player gets for surviving
		end
	end
end

Hope this helped.

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