I have an issue related to the PlayerRemoving process

Hello, if i need to explain what im trying to do in general. I have a “Players” folder. When a player touches a part, it creates an ObjectValue in this folder, and i change this objectvalue Value as shown in the code provided below

local Tag = Instance.new(ObjectValue)
Tag.Value = hit.Parent
Tag.Parent = PlayersFolder

The issue starts here. After this player exits the game, i want to delete this created ObjectValue, but the code i typed is not working, mine current script that i tried for this

Players.PlayerRemoving:Connect(function(player)
	for _, value in pairs(PlayersFolder:GetChildren()) do
		if value.Value == player.Character then
			value:Destroy()
			UpdateGui()
		end
	end
end)

Can you help me?

try this

Players.PlayerRemoving:Connect(function(player)
	for _, value in pairs(PlayersFolder:GetChildren()) do
                if value:IsA("ObjectValue")==false then
                        continue
                end
		if value.Value == player.Character then
			value:Destroy()
			UpdateGui()
		end
	end
end)
1 Like

Sadly it didnr work, and i also realised something 5 mins ago. When i try to print player.Character, i got “nil” i think value.Value is correct but there seems to be an issue with player.Character. Im not sure why this is happening

its because when PlayerRemoving is fired it also removes player character

1 Like

script that adds the value

local Tag = Instance.new("ObjectValue")
Tag.Name="PlayerChar"..hit.Parent.Name
Tag.Value = hit.Parent
Tag.Parent = PlayersFolder

script that removes the value

Players.PlayerRemoving:Connect(function(player)
	for _, value in pairs(PlayersFolder:GetChildren()) do
                if value:IsA("ObjectValue")==false then
                        continue
                end
		if value.Name=="PlayerChar"..player.Name then
			value:Destroy()
			UpdateGui()
		end
	end
end)
1 Like

This would make sense because when your player leaves the game, the character is deleted from the player, which results in the character not existing. If you want, you can clone the character if you need the character to stay back.

1 Like

Instead of finding the character, you can just set the object value’s name to the player’s name and after they leave you can simply find the objectvalue matching the player’s name and delete it

1 Like

Hi @Yeniay38

You could do this:

local Tag = Instance.new(ObjectValue)
Tag.Value = hit.Parent
Tag.Name = hit.Parent.Name -- add this line to rename the ObjectValue to the player's name
Tag.Parent = PlayersFolder
Players.PlayerRemoving:Connect(function(player)
	for _, value in pairs(PlayersFolder:GetChildren()) do
		if value.Name == player.Name then -- change to condition, so if the value.Name property is equal to the name of the player that is leaving
			value:Destroy()
			UpdateGui()
		end
	end
end)
1 Like

Sorry i fell asleep yesterday im answering now. I tried it and yes it fixed the problem. Thanks for help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.