Players.PlayerRemoving not working

Pretty much I have a Cleanup script that destroys a team leaders team and all of its data when the team leader leaves. Its supposed to remove all of the members of that team and place them on an already added neutral team while all of the other data of that team(folder, and a boolvalue) is destroyed and changed.

I don’t know if the issue is me being in test play which I’m testing the script on using a server and 2 players but I havent tested it outside of roblox studio.

Heres my code and no errors are output as far as I know but the script just doesn’t do anything.

local Players = game:GetService("Players")

Players.PlayerRemoving:Connect(function(player)
	if player.Data.isTribeLeader.Value == true then
		if player.Data.isInTribe.Value == true then
			
			wait(1)
		

local TribeMembers = player.Team:Getplayers()
			
			local PlayerTribeFolder = game.Workspace.Tribes:FindFirstChild(player.Team.Name)
					PlayerTribeFolder:Destroy()

					local TribesTakenFolder = game.Workspace.Tribes.TribesTaken
					local PlayerTribe = TribesTakenFolder:FindFirstChild(tostring(player.Data.tribeColor.Value))

					PlayerTribe.Value = false -- Set the tribes vacancy to false so other players can create it


					TribeMembers.Team = "Neutral" -- Set the members of the tribes team to neutral since leader left

					PlayerTribeFolder:Destroy() -- Destroy the tribes data folder since its no longer needed

					player.Team:Destroy() -- Destroy the players team so there isnt an empty team
				

	
			
		end
		end
end)

Images of the data that needs to be destroyed/changed:
image

the Tribes[“Green Tribe”] is supposed to be deleted while the TribesTaken.Green.Value is supposed to be set to false.

If you have any other questions about the data involved let me know but I have no idea why it isn’t working.

I don’t see why players.PlayerRemoving wouldn’t be working, try putting Print(“some text”) around your code to see if it stops anywhere, or if something is failing.

1 Like

Heres a video of the demonstration:

As you can see nothing is output and none of the folders are being changed/removed

Code I have currently:

local Players = game:GetService("Players")

Players.PlayerRemoving:Connect(function(player)
	if player.Data.isTribeLeader.Value == true then
		if player.Data.isInTribe.Value == true then
			
			wait(1)
		

local TribeMembers = player.Team:Getplayers()
			
			local PlayerTribeFolder = game.Workspace.Tribes:FindFirstChild(player.Team.Name)
					PlayerTribeFolder:Destroy()

					local TribesTakenFolder = game.Workspace.Tribes.TribesTaken
					local PlayerTribe = TribesTakenFolder:FindFirstChild(tostring(player.Data.tribeColor.Value))

					PlayerTribe.Value = false -- Set the tribes vacancy to false so other players can create it


					TribeMembers.Team = "Neutral" -- Set the members of the tribes team to neutral since leader left

					PlayerTribeFolder:Destroy() -- Destroy the tribes data folder since its no longer needed

					player.Team:Destroy() -- Destroy the players team so there isnt an empty team
				
print("Cleaned team")
	
			
		end
		end
end)

Put a print after every If statement, I’m sure that players.PlayerRemoving is running.

1 Like

Did as you said and placed a print after every if statement but still nothing is being output which brings me back to my first theory if its a result of me being in test play.

(The cleanup script is a serverscript inside of a folder in serverscriptservice)

Demonstration: player.Removing 2 - YouTube
(Video may still be processing, give it a minute)

Modified Code:

local Players = game:GetService("Players")

Players.PlayerRemoving:Connect(function(player)
	if player.Data.isTribeLeader.Value == true then
		print("Player is tribe leader")
		if player.Data.isInTribe.Value == true then
			print("Player is in tribe")
			
			wait(1)
		

local TribeMembers = player.Team:Getplayers()
			
			local PlayerTribeFolder = game.Workspace.Tribes:FindFirstChild(player.Team.Name)
					PlayerTribeFolder:Destroy()

					local TribesTakenFolder = game.Workspace.Tribes.TribesTaken
					local PlayerTribe = TribesTakenFolder:FindFirstChild(tostring(player.Data.tribeColor.Value))

					PlayerTribe.Value = false -- Set the tribes vacancy to false so other players can create it


					TribeMembers.Team = "Neutral" -- Set the members of the tribes team to neutral since leader left

					PlayerTribeFolder:Destroy() -- Destroy the tribes data folder since its no longer needed

					player.Team:Destroy() -- Destroy the players team so there isnt an empty team
				
print("Cleaned team")
	
			
		end
		end
end)

This would mean the condition below is failing, since the earliest time you placed a print is after that condition. PlayerRemoving is definitely running. Try place a print above everything (but below the .PlayerRemoving:Connect()). It’s definitely running. So like this:

Players.PlayerRemoving:Connect(function(player)
print("HERE")
	if player.Data.isTribeLeader.Value == true then

P.S: Still reading through the code, so don’t mind me. Don’t suppose you could get a screenshot of the objects in the player? I notice you use :Getplayers() on player.Team, just want to know what ‘Team’ is.

2 Likes

You’re indexing a table when you should be indexing the contents within that table.

local players = game:GetService("Players")

players.PlayerRemoving:Connect(function(player: Player)
	local tribeTeamActive, tribeTeam = pcall(function()
		return player.Team
	end)
	local wasInTribe, wasTribeLeader, tribeColor = pcall(function()
		return player.Data.isTribeLeader.Value, player.Data.tribeColor.Value
	end)
	if tribeTeamActive and wasInTribe and wasTribeLeader then
		task.wait(1)
		for _, player in pairs(tribeTeam:GetPlayers()) do
			player.Team = nil
			player.Neutral = true
		end
		workspace.Tribes[tribeTeam.Name]:Destroy()
		workspace.Tribes.TribesTaken[tribeColor].Value = false
	end
end)
1 Like

Sure thing. I added that print statement and now I finally have an error output which may go in hand with the comment under yours.

Heres the data inside the folder:
image

The object defined as “team” is this
image

What is line 14? Can you send the code.

Thank you, your code works for the most part but the following “errors” occured:
The tribe was not destroyed
Another team instance was created instead of being added to the already created team which is the Brown Neutral team. Other than that, the tribe folder was destroyed and the “Blue” value was set to false.

image

The tribe may not have exited, and I expected that when writing the code but my assumptions lead me to believe you would manage that bit.

1 Like

Alright, first thing I notice is as I thought, :Getplayers() is invalid, because you’re forgetting the upper case P, it should be :GetPlayers(). The full line (on line 14) should be like:

local TribeMembers = player.Team:GetPlayers()

This should make it so TribeMembers is populated with the players of the team that ‘player’ is in now.

2 Likes

As a result of @04nv 's comment I have readjusted my code to what he has provided and for the most part it works other than destroying the team which Im going to try and fix.

But as of now this is my current code which is outputting no errors thankfully

local Players = game:GetService("Players")

local players = game:GetService("Players")

players.PlayerRemoving:Connect(function(player: Player)
	local tribeTeamActive, tribeTeam = pcall(function()
		return player.Team
	end)
	local wasInTribe, wasTribeLeader, tribeColor = pcall(function()
		return player.Data.isTribeLeader.Value, player.Data.tribeColor.Value
	end)
	if tribeTeamActive and wasInTribe and wasTribeLeader then
		task.wait(1)
		for _, player in pairs(tribeTeam:GetPlayers()) do
			player.Team = nil
			player.Neutral = true
		end
		workspace.Tribes[tribeTeam.Name]:Destroy()
		workspace.Tribes.TribesTaken[tribeColor].Value = false
	end
end)

You won’t need the first Players variable as it is unused, though the lowercase players variable is.

1 Like

Didn’t notice that thank you. As I said i’m going to try and fiddle around the with the code but I’m going to mark your comment as the solution since it got me where I needed to be. Thank you all for the influencing and helpful comments.

Consider looping for a change instead.

local players = game:GetService("Players")

players.PlayerRemoving:Connect(function(player: Player)
	local tribeTeamActive, tribeTeam = pcall(function()
		return player.Team
	end)
	local wasInTribe, wasTribeLeader, tribeColor = pcall(function()
		return player.Data.isTribeLeader.Value, player.Data.tribeColor.Value
	end)
	if tribeTeamActive and wasInTribe and wasTribeLeader then
		task.wait(1)
		for _, player in pairs(tribeTeam:GetPlayers()) do
			player.Team = nil
			player.Neutral = true
		end
		for _, tribe in pairs(workspace.Tribes:GetChildren()) do
			if tribe.Name == tribeTeam.Name then
				tribe:Destroy()
			end
		end
		workspace.Tribes.TribesTaken[tribeColor].Value = false
	end
end)
2 Likes