I have this hunger bar that relies on a NumberValue inside of the player’s LocalPlayer to function. If you change the number in that NumberValue, the hunger bar GUI will change accordingly. I want to be able to save the NumberValue’s number so that the remaining hunger saves to the player’s next session on the game. I tried following this tutorial: Roblox DataStore Tutorial - Data Stores & Saving Data - Scripting Tutorial - YouTube, but it was saving something else, so I got confused. How do I make this code save a NumberValue in the Player?
local DataStoreService = game:GetService("DataStoreService")
local HungerSaving = DataStoreService:GetDataStore("HungerSaving")
game.Players.PlayerAdded:connect(function(player)
local HungerValue = player.HungerVal
end)
game.Players.PlayerRemoving:connect(function(player)
HungerSaving:SetAsync(player.UserId, --idk what to put here)
end)
local DataStoreService = game:GetService("DataStoreService")
local HungerSaving = DataStoreService:GetDataStore("HungerSaving")
game.Players.PlayerAdded:connect(function(player)
local HungerValue = player.HungerVal.Value
end)
game.Players.PlayerRemoving:connect(function(player)
HungerSaving:SetAsync(player.UserId, player.HungerVal.Value)
end)
Don’t forget the .Value on line 6, also somewhere inside the PlayerAdded function, you will need to use GetAsync() to get the data from the datastore
You mean like this?
local DataStoreService = game:GetService("DataStoreService")
local HungerSaving = DataStoreService:GetDataStore("HungerSaving")
game.Players.PlayerAdded:connect(function(player)
local HungerValue = player.HungerVal.Value
local Data
local Success, ErrorMessage = pcall(function()
Data = HungerSaving:GetAsync(player.UserId, player.HungerVal.Value)
end)
if Success then
HungerValue = Data
else
print("There was an error while getting data")
warn(ErrorMessage)
end
end)
game.Players.PlayerRemoving:connect(function(player)
local Success, ErrorMessage = pcall(function()
HungerSaving:SetAsync(player.UserId, player.HungerVal.Value)
end)
if Success then
print("Player Data succesfully saved")
else
print("There was an error when saving data")
warn(ErrorMessage)
end
end)
Also I got this error for line 6: HungerVal is not a valid member of Player "Players.asunoandkrito"
Hopefully this works, but you might have to do a little tweaking to it
local DataStoreService = game:GetService("DataStoreService")
local HungerSaving = DataStoreService:GetDataStore("HungerSaving")
game.Players.PlayerAdded:connect(function(player)
local HungerValue = Instance.new("NumberValue",player)
HungerValue.Name = "HungerVal"
HungerValue.Value = HungerSaving:GetAsync(player.UserId).HungerVal
local Data
local Success, ErrorMessage = pcall(function()
Data = HungerSaving:GetAsync(player.UserId).HungerVal
end)
if Success then
HungerValue.Value = Data
else
print("There was an error while getting data")
warn(ErrorMessage)
end
end)
game.Players.PlayerRemoving:connect(function(player)
local Success, ErrorMessage = pcall(function()
HungerSaving:SetAsync(player.UserId, player.HungerVal.Value)
end)
if Success then
print("Player Data succesfully saved")
else
print("There was an error when saving data")
warn(ErrorMessage)
end
end)
Datastores don’t save data that’s changed on the client.
I got this error on line 9: ServerScriptService.TestSaveHunger:10: attempt to index nil with 'HungerVal'
That means the number value doesn’t exist.
how do I save the number value then