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)
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)
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
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)
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.
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
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)