Yeah but I think we’re on the same page now. With a little bit of debugging you should be able to figure out the problem. My guess is that the player count changes too soon or too late leading to unexpected results (e.g. all players leave except one player but the player count is still two in the event from the player who just left being removed)
I just woke up. No, i made a while true loop that checks if there is 1 player, so that’s not the problem. The problem is that my value (game.ReplicatedStorage.vals.Winner) doesnt change to the last player’s alive name…
It gives me an error saying that 1 isnt a member of the team but I put that 1 so it changes the value to the player alive.
Script:
local playersInTeam = 0
while true do
wait()
for i, v in pairs(game.Players:GetChildren()) do
if v.Team == game.Teams.Game then
playersInTeam = playersInTeam + 1
end
end
if playersInTeam == 1 then
game.ReplicatedStorage.vals.Winner.Value = game.Teams.Game[1]
end
playersInTeam = 0
end
I think you’re making this very complicated. Let me just repeat what you are aiming to achieve.
You want to find out the name of the last player standing, then end the game.
If that’s what you’re trying to achieve, then I would say the title is somewhat misleading. Next time, I suggest you outline what you want to achieve clearly and have an appropriate title.
Why do I think the title is not relevant? It’s because you actually want to make the gameplay stop when there’s 1 player left standing in the game, not when there’s 0 players as the last player will have nobody to fight with.
Let’s get started
In this case, you want to use the GetPlayers function of Team, not the GetPlayers function of Players. They are different.
Like what @ProbableAI have written above, here’s how you would do it.
local inGameTeam = -- however you want to get a reference to it
inGameTeam.PlayerRemoved:Connect(function()
if #inGameTeam:GetPlayers() == 0 then
-- end the minigame
end
end)
But the twist is that we don’t actually want to end the game when there’s 0 player. We want it to end when there’s 1 player left standing, who is going to be the winner. So, we change the script to look like this.
local inGameTeam = -- however you want to get a reference to it
inGameTeam.PlayerRemoved:Connect(function()
if #inGameTeam:GetPlayers() == 1 then
-- end the minigame
end
end)
Ta da! That’s it, pretty much.
I was referring to the code you used above. The while loop is completely separate code and has no relevance. Like I said, I believe what is happening is there are still two players returned by GetPlayers in the Player removing event because the player is still considered part of the team. Like I also said, you should debug this code. It does not matter what other code you have. If specific code is not working as intended you should be debugging that code not making assumptions based on other code.
As for the new code you posted above, you should be able to figure out what is wrong on your own. The error clearly states “1” is not a valid member of team. You are indexing a team instance with a number. Additionally, in the full error message you have an exact line number and character position so you know exactly where the error is however you have not provided this to me. You can even click the error and see the exact place the error occurs.
Additionally, like I also said, you should be using GetService and WaitForChild instead of dot notation for most things. Using dot notation can lead to inconsistencies in errors and confusion on your part.
This problem is way trickier than it first seems!
Note this bug:
Here’s what you’ve got to do to properly garbage collect a team:
local function getRemainingPlayers(team, playerLeaving)
local players = {}
-- Handle players leaving but still being included in the list. This is due to this bug:
-- https://devforum.roblox.com/t/inconsistent-behavior-with-team-getplayers-when-called/194172
for _, player in pairs(self._team:GetPlayers()) do
if player ~= playerLeaving and player:IsDescendantOf(game) then
table.insert(players, player)
end
end
return players
end
local function teamIsDone(team, playerLeaving)
if not team:IsDescendantOf(Teams) then
-- Team must be parented to get players
-- Sometimes playerRemoved fires when the team is GCing, so we don't want to detect this!
return true
end
local remainingPlayers = getRemainingPlayers(playerLeaving)
if #remainingPlayers <= 0 then
return true
end
return false
end
local function onPlayerRemoved(playerLeaving)
if not teamIsDone(team, playerLeaving) then
-- TODO: Your code here!
end
end
-- Setup!
YOUR_TEAM_HERE.PlayerRemoved:Connect(onPlayerRemoved)
This certainly is unexpected. It should be called PlayerRemoving
instead to be consistent with Players. PlayerRemoved
implies past tense, meaning the player already has been removed, which is wrong.
Thanks for the help but I want to show the winner’s name as well not just end the game… That was my problem
Ok man I know most of the things u just said but I debbuged the script the first time. The only thing that doesnt work and that I dont know how to fix is how can i get the last player’s name and change the game.ReplicatedStorage.vals.Winner to that.
You’re checking for a player once all players have left, you can correct it by using the player argument passed in PlayerRemoved:
local inGameTeam = game.Teams.Game
inGameTeam.PlayerRemoved:Connect(function(player)
if #inGameTeam:GetPlayers() == 0 the
game.ReplicatedStorage.vals.Winner.Value = player.Name
-- Stop game
end
end)
So the player count check is working as intended? If so, the next step would be to try and figure out how to get the last player remaining. Really all you need to be doing is adding prints so you can see what’s happening. Print out values if they aren’t working as expected. Your case is not really what this forum is meant for.
Using the script that @slothfulGuy wrote, if you just add a few lines of code, it should work, like so.
local inGameTeam = -- however you want to get a reference to it
inGameTeam.PlayerRemoved:Connect(function()
if #inGameTeam:GetPlayers() == 1 then
local winnerName = #inGameTeam[1].Name -- looks inside the table of players in that team and gets the name of the remaining player(the winner)
print(winnerName) -- and now you have the name of the winner, printed out
-- and end the minigame here
end
end)
Hope this helped!
Edit: fixed some typos
Looks good , ill test it in a few minutes and mark it as a solution if its good. Thx to everyone for thr help!!!
It gives me an error saying that 1 isnt a member of that team. This is were I was stuck the last time too…
Like I said before, this is because you are indexing an instance with a number (1). Please refer to the line number and character position in the error or literally click the error. This is not difficult to figure out considering it’s as simple as clicking the error and reading the line it’s from, plus I’ve told you previously exactly what the issue is.
Additionally, I’ve already given you this information previously and you either ignored it or did not understand what I was asking you to do. The devforum is not here for us to debug and write scripts for you, it’s for you to learn how to do things on your own and for you to learn new things. The issue is simply that you are currently doing team[1] instead of team:GetChildren()[1]. Please read what I have already said prior, try to interpret it, and debug your own code by yourself.
All you need to really do to fix these issues on your own is place a few print statements and figure out what the error means. This issue really should have been solved days ago. All of the information you need is right here in front of you posted by numerous users and the solution has been here since then as well.
I apologize for getting worked up, but I’m a bit annoyed with this thread because I have been trying to help you for days now and you haven’t debugged your code and won’t take an effort to figure out what the error actually means. Please put effort in on your own. Again, we are not here to write code for you or debug for you. We learned on our own, and we want to help you learn too.
I knew the issue, I just didnt see your message. I knew that was what I had to change but I didnt know how. I`ve also been busy with homework so yeah. Anyways, thanks for helping and sorry if I made you upset…
Ok, I made it work but I used :GetPlayers() instead of :GetChildren() as that didn`t work.
Thanks to everyone for the help!!!
That was my mistake whoops. Good job finding it out! Additionally, I apologize for getting a bit mad. Honestly it wasn’t really your fault and I also should have been a bit more clear with my wording.
Your welcome! Sorry my script didn’t work, I forgot about the indexing