My script to announce multiple winners and award multiple players doesn't work

I made this script with help from other members but I just tested multiplayer and it doesn’t work. When this minigame is to finish and there is more than 1 person left, the game is supposed to announce all the winners and award them points. This is the part where the error comes in:


Also it pauses the game from moving on so in game it looks like this and it didn’t announce winners properly.

Got any ideas on what is going wrong?

The problem starts on line 152, The character is in the workspace, and you’re trying to get it from Players so ‘plr’ returned nil.
Also remove line 152 completely and on line 145 you can change ‘char’ to ‘plr’

1 Like

image

What eaxctly are you trying to do here?

Just do it like:

local char = plr.Character

He’s trying to get the player from the character:

https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayerFromCharacter

Yeah but he’s getting the player where he defined ‘char’ already, just needs to change ‘char’ to ‘plr’ and delete where he defined plr and his code should work fine

1 Like

This wouldn’t work as I am trying to get certain players that I are defined by v. The players aren’t all players, just certain ones. And also plr is defined later on and needs char to be valid so what your saying would simply just not work as plr wouldn’t yet be defined. The problem is at 153 and I think the nil has to do with the plr variable.

Here is a reference to the other part of the script that works if you need to see it to understand how it works image

You are using GetChildren() too many times for the same thing. Just do it once and store it in a variable. Then use the variable with the # to get the count. Like so:

local Children = workspace.Ingame:GetChildren()
if #Children >= 1 and rountType == "Randodeath" then
    if #Children == 1 then
         -- stuff
    elseif #Children > 1 then
       -- more stuff
    end
end

Also you can use:

plr.leaderstats.Wins.Value += 1

to add 1 to the value. instead of typing the whole thing over again.

My current way works and is good, this doesn’t really help in terms of getting the script to work. I need help with something other than my organizational skills.

It’s not necessarily about organization, its also a necessary optimization. It’s good practice to do it the way I wrote as it is what you would say the less laggy way to do it.

Also the error in output is because you didn’t check if leaderstats exist before attempting to add to the score.

That isn’t the problem though, I need help with something that my game doesn’t work without and instead you are distracting and getting into something else. I’ll clean it up later, but please, if you have any help make it on topic. I need help with something.

Look at the output error. It’s saying it couldn’t get the stats from a player, that’s why its stopping. Like I said in my previous post, you need to check if it exists (using FindFirstChild and checking if it returned true) before you attempt to add 1 to it.

Ok, also how would I list all of the players still alive too, I need help with that as well

you can use this function:

local function getPlayersAlive()
	local alive = {}
	for k,v in pairs(game.Players:GetPlayers()) do
		if v.Character then
			local humanoid = v.Character:FindFirstChild("Humanoid")
			if humanoid and humanoid.Health > 0 then
				alive[#alive+1] = v
			end
		end
	end
	return alive
end

local playersAlive = getPlayersAlive()

then the playersAlive variable will contain a table of players who are still alive at that moment. Which you can then put their names in a text label.

I’m not trying to get all of the live players, only the players alive who are in the Ingame Folder. This is because there will be dead players waiting in the lobby area watching

local function getPlayersAlive()
	local alive = {}
	for k,v in pairs(game.Players:GetPlayers()) do
		if v.Character and workspace.Ingame:FindFirstChild(v.Name) then
			local humanoid = v.Character:FindFirstChild("Humanoid")
			if humanoid and humanoid.Health > 0 then
				alive[#alive+1] = v
			end
		end
	end
	return alive
end

local playersAlive = getPlayersAlive()

This should do it. I made it check if their name is still in the Ingame in workspace.

Ok, how do I list out the live players? I list them out by setting status.Value equal to the winners and then add “Are the winners” or “is the winner” to list it on a screen GUI.

here is an example of the part that does work
local char = workspace.Ingame:FindFirstChildWhichIsA(“Model”)
status.Value = char.Name… " is the winner!!1!"

this one works only if there is 1 winner still alive.

using the playersAlive variable which contains a table of player objects, you can do:

local winners = "Winners: "
for k,v in pairs(playersAlive) do
	winners = winners..v.Name..", "
end
winners = winners:sub(1,#winners-2)
winners = winners.."!"

status.Value = winners

The reason I did the :sub(1,#winners-2) was because it removes the ", " at the end with “!”

I don’t get where to put this, can you explain a little more please?

Yeah I see how it works here, but this is because you’re finding the character model in the workspace in this code. In the first code where you have the error, you’re looking for the child with the player name in ‘Players’ , which should return the player already instead of a character that you’re searching for. Did you even try what I said to do?