Script breaking on respawn

Part of this Sanity script I made for my Backrooms game for some reason stops working when the player dies. It’s the portion that deals damage when Sanity hits 0. Does anyone know what’s causing this?

local Sanity = script.Sanity
--print("Sanity got")
Sanity.Value = 100
--print("Sanity set")
local maxSanity = script.maxSanity
--print("Max sanity got")
local player = game:GetService("Players").LocalPlayer
--print("Player got")
local character = player.Character or player.CharacterAdded:Wait()
--print("Character got")
local human = character:WaitForChild("Humanoid")
--print("Humanoid got")

print("Sanity variables initialised")

ServerScriptService = game:GetService("ServerScriptService")
blackout = game.Workspace.blackoutActive

print("Blackout value gotten")

while true do
	if Sanity.Value > maxSanity.Value then
		Sanity.Value = 100
	end
	if Sanity.Value > 0 and blackout.Value == false then
		print(Sanity.Value)
		task.wait(7)
		Sanity.Value -= 1
	elseif Sanity.Value > 0 and blackout.Value == true then
		print(Sanity.Value)
		task.wait(4)
		Sanity.Value -= 1
	elseif Sanity.Value < 1 then
		human.Health -= 5
		task.wait(2)
		human.Died:Connect(function()
			Sanity.Value = 100
		end)
	end
end
4 Likes

is the script in starterCharacterScripts? If not, you may want to put it there. And then you can redfine character as “script.Parent”

2 Likes

No, it’s in StarterPlayerScripts.

2 Likes

Well try putting it in starterCharatcerScript as I suggested.
If you can’t afford to put it outside of starterPlayerScripts, you could make a humanoid.Died event where you connect it to a function that updates the variable needed for the script

2 Likes

just want to make sure, is there any errors?

2 Likes

Try this. The humanoid becomes a nil instance on death in your script.

local Sanity = script.Sanity
--print("Sanity got")
Sanity.Value = 100
--print("Sanity set")
local maxSanity = script.maxSanity
--print("Max sanity got")
local player = game:GetService("Players").LocalPlayer
--print("Player got")
local character = player.Character or player.CharacterAdded:Wait()
--print("Character got")
local human = character:WaitForChild("Humanoid")
--print("Humanoid got")

player.CharacterAdded:Connect(function(char)
	character = char
	human = char:WaitForChild("Humanoid")
end)

print("Sanity variables initialised")

ServerScriptService = game:GetService("ServerScriptService")
blackout = game.Workspace.blackoutActive

print("Blackout value gotten")

while true do
	if Sanity.Value > maxSanity.Value then
		Sanity.Value = 100
	end
	if Sanity.Value > 0 and blackout.Value == false then
		print(Sanity.Value)
		task.wait(7)
		Sanity.Value -= 1
	elseif Sanity.Value > 0 and blackout.Value == true then
		print(Sanity.Value)
		task.wait(4)
		Sanity.Value -= 1
	elseif Sanity.Value < 1 then
		human.Health -= 5
		task.wait(2)
		human.Died:Connect(function()
			Sanity.Value = 100
		end)
	end
end
4 Likes

I’d recommend this:

--Script in ServerScriptService
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player: Player)
	Player:SetAttribute("Sanity", 100)
	Player:SetAttribute("MaxSanity", 100)
end)
--Script in StarterPlayer>StarterCharacterScripts
local Players = game:GetService("Players")
local Player = Players:GetPlayerFromCharacter(script.Parent)
local Sanity = 100
print("Variables initialized")

while true do
	if Sanity > Player:GetAttribute("MaxSanity") then
		Sanity = 100
		Player:SetAttribute("Sanity", Sanity)
	end
	if Sanity.Value > 0 and Player:GetAttribute("BlackoutActive") == false then
		--print(Sanity.Value)
		task.wait(7)
		Sanity -= 1
		Player:SetAttribute("Sanity", Sanity)
	elseif Sanity.Value > 0 and Player:GetAttribute("BlackoutActive") == true then
		--print(Sanity.Value)
		task.wait(4)
		Sanity -= 1
		Player:SetAttribute("Sanity", Sanity)
	elseif Sanity.Value < 1 then
		script.Parent.Humanoid:TakeDamage(5)
		task.wait(2)
	end
end

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