Script only working in Studio?

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!

https://gyazo.com/8a8dadda9cc4ac45f39ca4bc2dc86769 (studio)
https://gyazo.com/afe8c7bf32e5b23f7e9577f0e2d4a5f4 (ingame)

2 Likes

just out of curiosity, what CameraType is set in game?
image

Perhaps try setting it to Scriptable if it isn’t already

No obvious problems with the videos can you send the script

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)

1 Like

Can you add a print to onPlayerDeath and send the output on the Roblox client

prints in studio, doesn’t print ingame

Interesting the script may not be even running at all can you add a print right before the Player added:Connect line

I have to sleep I’ll look at this tmrw

prints in studio, doesnt print ingame

edit: nevermind, it does print ingame

Sorry about all the prints but can you replace the print before Player added:Connect with this line;

if Player.Character then
   print("yes")
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)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.