I made a respawn script where when the player dies, they stay dead and does some FOV stuff, when they click respawn they respawn
When playing the game published, it won’t work, the player just stays dead.
I’ve tried looking on the forums but I can’t find anything to resolve my problem, I have tried looking for scripts with “require” or anything else in them, I don’t have team create on either!
local Player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local TweenService = game:GetService("TweenService")
local function onPlayerDeath()
wait(1)
local guiTemplate = ReplicatedStorage:WaitForChild("DeathScreen"):Clone()
guiTemplate.Parent = Player.PlayerGui
local Camera = game.Workspace.CurrentCamera
local fovTweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Back, Enum.EasingDirection.InOut)
local fovTween = TweenService:Create(Camera, fovTweenInfo, {FieldOfView = 30})
fovTween:Play()
local soundGroup = SoundService:FindFirstChild("Soundtrack")
if soundGroup and soundGroup:IsA("SoundGroup") then
local sound = soundGroup:FindFirstChild("Gameover")
if sound and sound:IsA("Sound") then
sound:Play()
end
end
end
local function onCharacterAdded(character)
character:WaitForChild("Humanoid").Died:Connect(onPlayerDeath)
end
Player.CharacterAdded:Connect(onCharacterAdded)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RespawnEvent = ReplicatedStorage:WaitForChild("RespawnEvent")
RespawnEvent.OnClientEvent:Connect(function()
local Camera = game.Workspace.CurrentCamera
Camera.FieldOfView = 70
end)
If you have that local script in PlayerScripts, you need to connect it once the script runs, and the disconnection will disappear when the Humanoid gets deleted after dying.
Then you need to connect it again, perhaps using the CharacterAdded, but, that will fire for any players that spawns in game so need to compare that player that spawned is the same one that the owner of the script.
If you place the script in CharacterScripts, then that will script will be a new one each time the player dies and spawns, so you would connect the died event each time the script runs and no need to use the CharacterAdded event. You know that the character indeed got added cause the script starter to run.
So if you want to do it on PlayerScripts, that script will be never be deleted but the connection to the event and the Humanoid will. So do this:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local TweenService = game:GetService("TweenService")
----------------------------------------------------------------------
local RespawnEvent = ReplicatedStorage:WaitForChild("RespawnEvent")
----------------------------------------------------------------------
local fovTweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Back, Enum.EasingDirection.InOut)
local Players = game.Players
local Player = Players.LocalPlayer -- The player owner of the script
local Camera = game.Workspace.CurrentCamera
local Char = Player.Character or Player.CharacterAdded:Wait() -- Wait for player's character
local Hum = Char:WaitForChild("Humanoid", 30) -- wait for the humanoid
-- What to do when Humanoid died function:
local function onDie()
task.wait(1)
local guiTemplate = ReplicatedStorage:WaitForChild("DeathScreen"):Clone()
guiTemplate.Parent = Player.PlayerGui
TweenService:Create(Camera, fovTweenInfo, {FieldOfView = 30}):Play()
local soundGroup = SoundService:FindFirstChild("Soundtrack")
if soundGroup and soundGroup:IsA("SoundGroup") then
local sound = soundGroup:FindFirstChild("Gameover")
if sound and sound:IsA("Sound") then
sound:Play()
end
end
end
local dieConn = false -- Holds the Died event Connection to the function
-- Connect the Died event once the script runs by first time
if Hum then
dieConn = Hum.Died:Connect(onDie)
else
warn("Humanoid never did show")
end
-- Connect the Died event each time player respawn
Player.CharacterAdded:Connect(function(char) -- This fires for any player that respawns in game so...
if Players:GetPlayerFromCharacter(char) == Player then -- check it only runs for the owner of this script
Hum = char:WaitForChild("Humanoid", 30)
Camera.FieldOfView = 70
if dieConn then
dieConn:Disconnect()
dieConn = false
end
dieConn = Hum.Died:Connect(onDie) -- Connect the Died event again
end
end)