Delete local part on death

I am currently unable to find a way to detect when a player died then delete a part that has been made locally. I tried to do this by having a server script detect when a player dies, then fire a remotevent to their client. They would have a gui which then listens for the event and then deletes the local part which has been made elsewhere in the script.

local script

			game:GetService("ReplicatedStorage").InitiateMission:FireServer(script.Parent.Parent.Quest.Value)
			local item = game.ReplicatedStorage.MissionItems:FindFirstChild(script.Parent.Parent.Quest.Value):Clone()
			item.Parent = game.Workspace
			local designatedspawn = game.Workspace.StaticSpawns.Missions.SpawnPoints:FindFirstChild(script.Parent.Parent.Quest.Value):GetChildren()[math.random(1,#game.Workspace.StaticSpawns.Missions.SpawnPoints:FindFirstChild(script.Parent.Parent.Quest.Value):GetChildren())]
			item:MoveTo(designatedspawn.Position)
			script.Parent.Text = "Not finished"
			script.Parent.Parent.InProgress.Text = "In Progress"
			clone = script.LocalScript:Clone()
			clone.Parent = game.Players.LocalPlayer.PlayerGui
			clone.Name = script.Parent.Parent.Quest.Value
			clone.Disabled = false
		end
	end
end)

game.ReplicatedStorage.PlayerDead.OnClientEvent:connect(function()
	clone:Destroy()
end)

server script

	plar.CharacterAdded:connect(function(chr)
		chr:WaitForChild("Humanoid").Died:connect(function()
			plar.InMission.Value = false
			for i, v in pairs (plar.Missions:GetChildren()) do
				v:Destroy()
			end
			game.ReplicatedStorage.PlayerDead:FireClient(plar)
		end)
	end)
end)

You don’t need a serverscript for this at all in that case.

Just replace game.ReplicatedStorage.PlayerDead.OnClientEvent:connect(function() with game.Players.LocalPlayer.Character.Humanoid.Died:Connect(function().

It doesnt do it I tried,

 game.Players.LocalPlayer.Character:WaitForChild("Humanoid").Died:connect(function()
	clone:Destroy()
end)

Is there a while-loop or something else that would loop forever above it?
Is there an error that shows when you do it this way?

you can use if Humanoid health == 0

No:

clone = nil

script.Parent.MouseButton1Click:connect(function()
	if game.Players.LocalPlayer.Missions:FindFirstChild(script.Parent.Parent.Quest.Value) then
	if game.Players.LocalPlayer.InMission.Value == true then
		if game.Players.LocalPlayer.Missions:FindFirstChild(script.Parent.Parent.Quest.Value).Value == true then
			game.ReplicatedStorage.MissionEnd:FireServer(script.Parent.Parent.Quest.Value)
			game.Workspace:FindFirstChild(script.Parent.Parent.Quest.Value):Destroy()
			script.Parent.Text = "Replay Mission"
			script.Parent.Parent.InProgress.Text = ""
		end
	end
	else
		if game.Players.LocalPlayer.InMission.Value == false then
			game:GetService("ReplicatedStorage").InitiateMission:FireServer(script.Parent.Parent.Quest.Value)
			local item = game.ReplicatedStorage.MissionItems:FindFirstChild(script.Parent.Parent.Quest.Value):Clone()
			item.Parent = game.Workspace
			local designatedspawn = game.Workspace.StaticSpawns.Missions.SpawnPoints:FindFirstChild(script.Parent.Parent.Quest.Value):GetChildren()[math.random(1,#game.Workspace.StaticSpawns.Missions.SpawnPoints:FindFirstChild(script.Parent.Parent.Quest.Value):GetChildren())]
			item:MoveTo(designatedspawn.Position)
			script.Parent.Text = "Not finished"
			script.Parent.Parent.InProgress.Text = "In Progress"
			clone = script.LocalScript:Clone()
			clone.Parent = game.Players.LocalPlayer.PlayerGui
			clone.Name = script.Parent.Parent.Quest.Value
			clone.Disabled = false
		end
	end
end)

game.Players.LocalPlayer.Character:WaitForChild("Humanoid").Died:connect(function()
	clone:Destroy()
end)

That is all the script

When the script runs, there is no guarantee that the player has a character. Instead, do this:

local Character = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").CharacterAdded:Wait()
Character:WaitForChild("Humanoid").Died:Connect(function()
clone:Destroy()
end)

You need to connect to the death event on the client. You can do this easily by placing a local script in StarterCharacterScripts which waits for their humanoid and then connects to the Died event.

Im already doing this its a localscript inside a texbutton

and the ui resets on spawn, right?

Yes but i tried a script in startercharacter scripts it also didnt work

Try this:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

hum.Died:Connect(function()
    if clone then
        clone:Destroy()
    end
end)

Also, did you have any errors in any of the scripts used above? If you also have errors in this script, please let me know

None of them errors it just doesnt even do anything… like even if you put a print after the .Died event it doesnt even print

Do you have any loops that yields the script before it reaches the event? Are you sure the script isn’t disabled?

Theres no loops and the script does not become disabled

Even put this script in StarterCharacterScripts

local Character = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").CharacterAdded:Wait()
	Character:WaitForChild("Humanoid").Died:Connect(function()
		for i,v in pairs(game.Players.LocalPlayer.Missions:GetChildren()) do
			if game.Workspace:FindFirstChild(v.Value) then
				game.Workspace:FindFirstChild(v.Value):Destroy()
			end
		end
end)

And it doesn’t do anything? Could you show the hierarchy of the local script in the textbutton? Have you tried putting a print at the top of the script or before the connection to see if it’s working at all?


I don’t see what your problem entirely is??

local hum = script.Parent:WaitForChild('Humanoid')

if hum then
	hum.Died:Connect(function()
		print('oh noes he died')
		workspace.Baseplate.BrickColor = BrickColor.Red()
	end)
end

image
Since it is in StarterCharacter, it is parented to every new Character created. Thus, making it work every single time without failure.

local hum = script.Parent:WaitForChild('Humanoid')

if hum then
	hum.Died:Connect(function()
		for i,v in pairs(game.Players.LocalPlayer.Missions:GetChildren()) do
			if game.Workspace:FindFirstChild(v) then
				game.Workspace:FindFirstChild(v):Destroy()
			end
		end
	end)
end	

it detects when the humanoid dies fine

But I want to index through all the items in the player’s folder then delete the in workspace with the same name but it doesn’t do this

Would you show a picture of the hierarchy? i.e
image