How would I make a value save after death?

Hello, I’m trying to make a script that keeps your value after death. But I have a problem, the script doesn’t even work.

local tool = script.Parent
local toolValue = tool.ToolType
local humanoid = tool.Parent:FindFirstChild("Humanoid") or tool.Parent.Parent.Character:FindFirstChild("Humanoid")
local waitTime = game.Players.RespawnTime + 1

local function death()
	local recentValue = toolValue.Value
	wait(waitTime)
	toolValue.Value = recentValue
end

humanoid.Died:Connect(death)

I have tried looking at other Developer Forum posts about this and I can’t seem to find a solution.

1 Like

I cannot test your script right now, but add

print("This part worked")

This will allow you to see where it is breaking. Do you have your output opened, are there any errors?

1 Like

It doesn’t print after the wait()

1 Like

Well if you die with a tool, it is destroyed and the scripts are deleted. You could save the values into the player instead.

1 Like

There’s a variety of ways to do this but I recommend a simple module script. When the Humanoid dies you need to pass the value into a RemoteEvent. Then on the server handle the event firing and you can have a module that stores the data e.g.:

local ToolData = {}

ToolData.Players = {} -- Key will be a player instance, value will be the tool value

function ToolData.StoreData(player, data)
  ToolData.Players[player] = data
end

function ToolData.GetData(player) -- Might as well as make a getter as well
  return ToolData.Players[player]
end


return ToolData

Your server script handling your event would look something like:

local ToolData = require(game:GetService('ReplicatedStorage').ToolData)

SaveDataEvent.OnServerEvent:Connect(function(player, data)
  ToolData.StoreData(player, data)
end)

-- You can also create a RemoteFunction for getting the data
GetDataFunction.OnServerInvoke = function(player)
  return ToolData.GetData(player)
end
1 Like

I still have not learned about ModuleScripts. Could you please tell me what is going on in the scripts?

1 Like

This solution might not be for you then. What you could do is instead create a StringValue or whatever value your data is and store the data in there when the player dies. Remember to create it on the server as if it is made on the client it will remain client-side and unaccessible to the server so you’ll still have to use RemoteEvents. Here’s the offical Roblox dev article explaining ModuleScripts in detail. They’re basically scripts that act as containers of data. They’re extremely useful and used frequently in most games.

1 Like

I have found a solution to this.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local tool = player.Backpack:FindFirstChildWhichIsA("Tool") or character:FindFirstChildWhichIsA("Tool")
		
		character.Humanoid.Died:Connect(function()
			local recentValue = tool.ToolType.Value
			wait(game.Players.RespawnTime + 0.1)
			tool = player.Backpack:FindFirstChildWhichIsA("Tool")
			tool.ToolType.Value = recentValue
		end)
	end)
end)
1 Like