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.
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
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.