How to save a boolean's value after humanoid died?

Hello developers!

I ran into an issue when scripting.

So if a boolean value is true, after the humanoid dies, this resets. Is there any way to make the boolean’s value save even after death?

Here is my script:

local off = script.Parent.Parent.Parent.Parent.off.Value
local player = game.Players.LocalPlayer
local character = player.Character

character:WaitForChild("Humanoid").Died:Connect(function()
	print("Died!")
	if game.ReplicatedStorage:FindFirstChild("CharacterModels") then
		game.ReplicatedStorage.CharacterModels.Parent = workspace
	end
end)

script.Parent.MouseButton1Down:Connect(function()
	if off == false then
		workspace.CharacterModels.Parent = game:GetService("ReplicatedStorage")
		script.Parent.BackgroundColor3 = Color3.fromRGB(250, 0, 0)
		off = true
	elseif off == true then
		game.ReplicatedStorage.CharacterModels.Parent = workspace
		script.Parent.BackgroundColor3 = Color3.fromRGB(0, 200, 23)
		off = false
	end
end)
1 Like

Assuming it deletes because the value is in the character, put the value in the player instead?

2 Likes

I simply changed the value by making it an Instance.new; the value is now a child of Player, but still for some reason keeps resetting

It resets after the player dies. I recommend storing it in the player like @Orbular3 stated. It’s more reliable that way.

1 Like

Alright, I just tested it. I see the problem now. So storing it in the player does work, its just that when the player dies, it creates a duplicate of the value which is reset. In other words, the original value is still the same (for example i set it to true, and it saves), but the copy is false, and it’s causing an error in my script.

Here is my new script:

local player = game.Players.LocalPlayer
local character = player.Character
local off = Instance.new("BoolValue", player)

script.Parent.MouseButton1Down:Connect(function()
	if off.Value == false then
		workspace.CharacterModels.Parent = game:GetService("ReplicatedStorage")
		script.Parent.BackgroundColor3 = Color3.fromRGB(250, 0, 0)
		off.Value = true
	elseif off.Value == true then
		game.ReplicatedStorage.CharacterModels.Parent = workspace
		script.Parent.BackgroundColor3 = Color3.fromRGB(0, 200, 23)
		off.Value = false
	end
end)

First, I see you are declaring the parent in the Instance.new statement. Roblox recommends against this, as it causes all sorts of replication issues down the line. They recommend setting the parent last. This isn’t your problem though, but while you are there! :wink:

I recommend looking for the BoolValue before creating a new one. That should avoid the duplicates issue.

If you want this value in the character instead, you have to prevent the character’s death (hp = 1) long enough to grab the value. Then let the character die, and in a CharacterAdded event, put the value back in. This assumes you will know in advance when a character is going to die.

Instance (roblox.com)

1 Like
  1. Using the Parent property is not recommended but isn’t harmful, just super inefficient when the server has to perform physics calculations and the like 20 times for a part that you rapidly changed after it popped into existence.

  2. As mentioned above, check for the bool value to exist.

local off = player:FindFirstChild("BoolValue") or Instance.new("BoolValue", player)
-- you should really name the bool value, but you haven't as of now
  1. The above is not true. Unless if you have configured it to do so, the player and humanoid is destroyed several seconds after the player dies. However, #2 is the much better solution.
1 Like

Try this:

local off = script.Parent.Parent.Parent.Parent.off
local player = game.Players.LocalPlayer
local character = player.Character
local Data = nil

character:WaitForChild("Humanoid").Died:Connect(function()
	print("Died!")
	if game.ReplicatedStorage:FindFirstChild("CharacterModels") then
		game.ReplicatedStorage.CharacterModels.Parent = workspace
        Data = off.Value
	end
end)

player.CharacterAdded:Connect(function(char)
wait(0.4)
 if Data ~= nil then
  off.Value = Data
 end
end)
script.Parent.MouseButton1Down:Connect(function()
	if off == false then
		workspace.CharacterModels.Parent = game:GetService("ReplicatedStorage")
		script.Parent.BackgroundColor3 = Color3.fromRGB(250, 0, 0)
		off = true
	elseif off == true then
		game.ReplicatedStorage.CharacterModels.Parent = workspace
		script.Parent.BackgroundColor3 = Color3.fromRGB(0, 200, 23)
		off = false
	end
end)
1 Like
  1. The above is not true. Unless if you have configured it to do so, the player and humanoid is destroyed several seconds after the player dies. However, #2 is the much better solution.

What configuration keeps the humanoids around? That’s not default behavior for me. My humanoids do not stick around after a player/NPC dies. I have all sorts of code in my combat system to shut down animations and attacks that have been queued up in the seconds following death. The animator is gone, .Health is gone, and every queued event is a script error.

1 Like

Thank you all for the reply, I appreciate all of your responses. @kaz_iaa @GolgiToad , I tried checking for the boolvalue to exist already, yet there is still a duplication error. Any way I can try to fix this? Again thank you all.

Okay, so I managed to fix it temporarily, until the second time of death where it breaks. Here is what i added.

Local Script:

local player = game.Players.LocalPlayer
local character = player.Character
local off = player:FindFirstChild("BoolValue") or Instance.new("BoolValue", player)
local humanoid = character:WaitForChild("Humanoid")

off.Name = "off"

humanoid.Died:Connect(function()
	print("Died")
	game.ReplicatedStorage.SaveValueEvent:FireServer()
end)

game.ReplicatedStorage.SaveValueEvent.OnClientEvent:Connect(function()
	if player:FindFirstChild("off").Value == true then
		for i,v in pairs(player:GetChildren()) do
			if v.Name == "off" then
				v.Value = true
				print("Success!")
			end
		end
	elseif player:FindFirstChild("off").Value == false then
		for i,v in pairs(player:GetChildren()) do
			if v.Name == "off" then
				v.Value = false
				print("Success!")
			end
		end
	end
end)

script.Parent.MouseButton1Down:Connect(function()
	if off.Value == false then
		workspace.CharacterModels.Parent = game:GetService("ReplicatedStorage")
		script.Parent.BackgroundColor3 = Color3.fromRGB(250, 0, 0)
		off.Value = true
	elseif off.Value == true then
		game.ReplicatedStorage.CharacterModels.Parent = workspace
		script.Parent.BackgroundColor3 = Color3.fromRGB(0, 200, 23)
		off.Value = false
	end
end)

Server Script:

game.ReplicatedStorage.SaveValueEvent.OnServerEvent:Connect(function(player)
wait(4)
game.ReplicatedStorage.SaveValueEvent:FireClient(player)
end)

Again, after the third time I die, it breaks, with the error message, "


"

You are searching for “BoolValue”… after you already renamed it “off”. Try FindFirstChild(“off”)