--
--
local DeathGui = plr.PlayerGui.DeathGui
local Event = game.ReplicatedStorage.RemoteEvents.PlayerDied
local function onCharacterAdded(Char)
local Human = Char:WaitForChild("Humanoid")
Human.Died:Connect(function()
--
--
end
plr.CharacterAdded:Connect(onCharacterAdded)
local plr = game:GetService("Players").LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local TS = game:GetService("TweenService")
local TweenInfo1 = TweenInfo.new(6)
local connection
local DeathGui = plr.PlayerGui.DeathGui
local Event = game.ReplicatedStorage.RemoteEvents.PlayerDied
local Human = Char:WaitForChild("Humanoid")
local function Died()
plr:SetAttribute("Dead", true)
TS:Create(DeathGui.DeathFrame, TweenInfo1, {BackgroundTransparency = 0}):Play()
task.wait(6.5)
DeathGui.DeathFrame.RestartButton.TextTransparency = 0
DeathGui.DeathFrame.RestartButton.Active = true
DeathGui.DeathFrame.RestartButton.MouseButton1Click:Connect(function()
Event:FireServer(plr, Char)
print("done")
local HumRootPart = Human.Parent:WaitForChild("HumanoidRootPart")
print("FOUND")
local SpawnPoints = {
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart1"),
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart2"),
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart3"),
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart4"),
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart5"),
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart6")
}
local ChosenSpawnNumber = math.random(1, #SpawnPoints)
print(ChosenSpawnNumber)
local ChosenSpawn = SpawnPoints[ChosenSpawnNumber]
print(ChosenSpawn)
game:GetService("Players").LocalPlayer.Character:FindFirstChild("HumanoidRootPart").CFrame = ChosenSpawn.CFrame
end)
if connection then connection:Disconnect() end
Char = plr.CharacterAdded:Wait()
Human = Char:WaitForChild("Humanoid")
connection = Human.Died:Connect(Died)
end
connection = Human.Died:Connect(Died)
Make sure to have the local script inside of the StarterCharacterScripts
folder instead of the StarterPlayerScripts folder. This is, because of when the player respawns, a new character object is created, with a new humanoid object in it. Therefore, the Human.Died event won’t run again, as the old humanoid doesn’t exist anymore. Hope this will work!
you need to wrap the Humanoid.Died() event inside of a CharacterAdded event, this makes it so that whenever a new Character is added (or when you respawn) it connects the Died event for that new Character:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:FindFirstChild("Humanoid")
humanoid.Died:Connect(function()
-- do the code you want to run when a player dies here
end)
end)
end)
How about this, instead of teleporting inside of the Humanoid.Died, how about you do it when the character respawns?
-- at the top of your script
if plr:GetAttribute("Respawning") == true then
plr:SetAttribute("Respawning", false)
local SpawnPoints = {
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart1"),
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart2"),
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart3"),
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart4"),
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart5"),
workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart6")
}
local ChosenSpawnNumber = math.random(1, #SpawnPoints)
print(ChosenSpawnNumber)
local ChosenSpawn = SpawnPoints[ChosenSpawnNumber]
print(ChosenSpawn)
plr.Character:WaitForChild("HumanoidRootPart").CFrame = ChosenSpawn.CFrame
end
Human.Died:Connect(function()
plr:SetAttribute("Respawning", true)
-- rest of your code besides the spawn point section
end)
EDIT: Explanation, when the player dies it sets an attribute (needed only on the client since that’s where the teleporting is happening unless you wanna move it on the server) which we then use on the next spawning of the player to know whether it’s a respawn or if the player has first spawned in. If the attribute is there then it’s a respawn and we teleport the player to a random spawn point, otherwise we ignore that section and let only the rest of the code run