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