Trouble with removing an instance from a player

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a player’s tag be destroyed if they die/reset.

  2. What is the issue? Include screenshots / videos if possible!
    The tag is not being removed and there are no errors.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I searched far and wide in both the developer hub and the forums but could find no applicable answer.

Here is my local script located in StarterPlayer > StarterCharacterScripts:

local char = script.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
local hum = char:WaitForChild("Humanoid")
hum.Died:Connect(function()
	if plr:FindFirstChild("Tag") then
		plr.Tag:Destroy()
	end
end)

All help is greatly appreciated!

2 Likes

Where does the tag come from? any other script?

Yes, a tag is generated from another script and placed into the player. The tags are currently BoolValues.

The game remove the tag, but when the player respawn the other script put it back

No. The tag is placed into the player, not the character.

can i see the function that add the tag?

for i, v in pairs(game.Players:GetPlayers()) do
			if not v:FindFirstChild("Tag") then
				local tag = Instance.new("BoolValue")
				tag.Name = "Tag"
				tag.Parent = v
				tag.Value = false
			end
		end
		local playerOn = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
		if playerOn.Tag then
			playerOn.Tag.Value = true
		else
			local tag = Instance.new("BoolValue")
			tag.Name = "Tag"
			tag.Parent = playerOn
			tag.value = true
		end

what if when the player die you just change the value and not delete the object?

The value is whether they are a seeker or not.

is the function that add the tag in a while loop or something that put it back?

Maybe try using for loop to find the “Tag”

hum.Died:Connect(function()
	for i,v in pairs(plr:GetDescendants()) do
		if v.Name == 'Tag' then
			print('Found')
			v:Destroy()
		else
			print('no tag found')
		end
	end
end)

and if it doesn’t find anything named ‘Tag’ maybe it’s not in there in the first place?

I tried this and it printed nothing.

I looked in the server window when testing the game with 2 players and it showed both players having tags.

add a print(“tag added” … player.Name) at the add function, then add a print(“tag removed” … player.Name) at the remove function… so we can see if it not delete the tag or it delete the tag but the other script put it back

Nevermind, found the solution myself. I forgot entirely that removing the Tag from local side wouldn’t show server side.

I think it’s because it’s a local script causing the trouble because:

  1. Everything under the player character is deleted including the local scripts inside it when the humanoid/player dies
  2. Local script cannot delete things in the explorer because of the filtering thing

Try using this server script instead
( Place this inside in a server script and put it in serverscriptservice )

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.Humanoid.Died:Connect(function()
			for i,v in pairs(plr:GetDescendants()) do
				if v.Name == 'Tag' then
					print('found tag')
					v:Destroy()
				else
					print('no tag found')
				end
			end
		end)
	end)
end)