How do I make my sanity script reset?

Hi there.

I have a script that allows a sanity meter and if you go inside of a ‘zone’, it’ll lower their sanity and make their vision become black and white, and their FOV is zoomed in. If they step out of the ‘zone’, their sanity will rise back up, allowing their vision to go back to normal. However, when you die, the sanity meter will remain and it will NOT reset- even when you die.

If anyone needs a video example, let me know, but you can easily see it if you play through the testing grounds I’ve made for my game.

I just need help putting this into my script, and there’s multiple scripts to make this system work- so if anyone could configure the game and show me what I did wrong, please let me know.

Some key things to know:

  • I am an amateur scripter, so I followed a tutorial on how to make this system.
  • I want to learn, but the best way for me to learn is if someone tells me where to put a certain line of code in my scripts and explains what it does to me.
  • When explaining on how to do certain things, please keep it simple so that way my brain can understand it.

Here is the place if anyone wants to playtest it and try to see what I did wrong. Thank you.
Testing Purposes - Vermiculus 1.1 (1).rbxl (70.5 KB)

1 Like

For the call of the function put:

game.Players.LocalPlayer.Character.Humanoid.Died:Connect(nameoffunction)

And then:

NameOfSanityMeter.Value = 100 -- replace 100 with whatever reset value that it starts off with

Thanks! I’ll let you know ASAP if this works.

I’m sure that this does work, but I am a bit confused where you put it exactly. Again, I did follow a tutorial and I’d like an explanation on what happens if I put this into the script so that way I can better understand it.

Anyway, this is the line of code for the sanity in the ServerScriptService, and I’m not too sure where to put that at.

local Settings = {
	ZoneRequired = true; 
	MinDecreaseTime = 1;
	MaxDecreseTime = 1;
	
}
local function SanityChecker(p, Action) 
	local Character = p.Character or p.CharacterAdded:Wait() 
	local Sanity = Character:WaitForChild("Sanity") 
	
	if Action == "CheckSanity" then 
		return Sanity.Value 

	end
end

for _, v1 in pairs(workspace.Zones:GetChildren()) do 
	if v1:IsA("BasePart") then 
		v1.Touched:Connect(function(h)
			local Humanoid = h.Parent:FindFirstChild("Humanoid") 
			if Humanoid then 
				local Player = game.Players:GetPlayerFromCharacter(h.Parent)
				h.Parent:WaitForChild("Unsafe").Value = true
				h.Parent.Zone.Value = v1.Name
			end
		end)
		
		v1.TouchEnded:Connect(function(h) 
			local Humanoid = h.Parent:FindFirstChild("Humanoid") 
			if Humanoid then
				local Player = game.Players:GetPlayerFromCharacter(h.Parent)
				h.Parent:WaitForChild("Unsafe").Value = false 
				h.Parent.Zone.Value = "" 
			end
		end)
	end
end

game.ReplicatedStorage.SanityHandler.OnServerInvoke = SanityChecker 

game.Players.PlayerAdded:Connect(function(p) 
	local Character = p.Character or p.CharacterAdded:Wait() 
	local Sanity = Character:WaitForChild("Sanity")
	local debounce1 = false
	
	spawn(function() 
		while not debounce1 and wait(math.random(Settings.MinDecreaseTime,Settings.MaxDecreseTime)) do 
			if Character.Unsafe.Value then 
				if Settings.ZoneRequired then 
					if Character.Zone.Value ~= nil and Character.Zone.Value ~= "" then 
						local module = require(game.ReplicatedStorage.Modules.Zones) 
						if module[Character.Zone.Value] then
							Sanity.Value -= module[Character.Zone.Value].decrease 
							if Sanity.Value < 0 then
								Sanity.Value = 0
							end
						else
							Sanity.Value -= 1 
							warn("Zone not found:",Character.Zone.Value)
							if Sanity.Value < 0 then
								Sanity.Value = 0
							end
						end
					end
				else
					Sanity.Value -= 1 
				end
			else
				if Character.Regen.Value then 
					Sanity.Value += 1 
					Sanity.Value = math.clamp(Sanity.Value,0,100)
				end
			end
		end
	end)
	
end)

Try this:

local function ResetSanity()
    if Sanity.Value =< 0 then
        Sanity.Value = 100
    end
end)

game.Players.LocalPlayer.Character.Humanoid.Died:Connect(ResetSanity)

This should be a new function that you have at the very bottom of your script above

Don’t know where you’d put that, and it’s still an error. The GUI I use for the sanity meter is just ‘Label’ now.

local Settings = {
	ZoneRequired = true; 
	MinDecreaseTime = 1;
	MaxDecreseTime = 1;
	
}
local function SanityChecker(p, Action) 
	local Character = p.Character or p.CharacterAdded:Wait() 
	local Sanity = Character:WaitForChild("Sanity") 
	local function ResetSanity()
		if Sanity.Value == 0 then
			Sanity.Value = 100
		end

	game.Players.LocalPlayer.Character.Humanoid.Died:Connect(ResetSanity)
	
	if Action == "CheckSanity" then 
		return Sanity.Value 

	end
end

for _, v1 in pairs(workspace.Zones:GetChildren()) do 
	if v1:IsA("BasePart") then 
		v1.Touched:Connect(function(h)
			local Humanoid = h.Parent:FindFirstChild("Humanoid") 
			if Humanoid then 
				local Player = game.Players:GetPlayerFromCharacter(h.Parent)
				h.Parent:WaitForChild("Unsafe").Value = true
				h.Parent.Zone.Value = v1.Name
			end
		end)
		
		v1.TouchEnded:Connect(function(h) 
			local Humanoid = h.Parent:FindFirstChild("Humanoid") 
			if Humanoid then
				local Player = game.Players:GetPlayerFromCharacter(h.Parent)
				h.Parent:WaitForChild("Unsafe").Value = false 
				h.Parent.Zone.Value = "" 
			end
		end)
	end
end

game.ReplicatedStorage.SanityHandler.OnServerInvoke = SanityChecker 

game.Players.PlayerAdded:Connect(function(p) 
	local Character = p.Character or p.CharacterAdded:Wait() 
	local Sanity = Character:WaitForChild("Sanity")
	local debounce1 = false
	
	spawn(function() 
		while not debounce1 and wait(math.random(Settings.MinDecreaseTime,Settings.MaxDecreseTime)) do 
			if Character.Unsafe.Value then 
				if Settings.ZoneRequired then 
					if Character.Zone.Value ~= nil and Character.Zone.Value ~= "" then 
						local module = require(game.ReplicatedStorage.Modules.Zones) 
						if module[Character.Zone.Value] then
							Sanity.Value -= module[Character.Zone.Value].decrease 
							if Sanity.Value < 0 then
								Sanity.Value = 0
							end
						else
							Sanity.Value -= 1 
							warn("Zone not found:",Character.Zone.Value)
							if Sanity.Value < 0 then
								Sanity.Value = 0
							end
						end
					end
				else
					Sanity.Value -= 1 
				end
			else
				if Character.Regen.Value then 
					Sanity.Value += 1 
					Sanity.Value = math.clamp(Sanity.Value,0,100)
					end
				
				end
			end
		end)
	
	end)

end

That wasn’t anything from my script

hi i replied you in the old post, if you got time read it , i fixed what i forgot , sorry for the issue

1 Like