Im trying to make a DataStore for all the values in ReplicatedStorage in my game. I get no error but none of the values save. help
local DataStoreService = game:GetService("DataStoreService") local inventory = DataStoreService:GetDataStore("Inventory") local values = game.StarterGui.ShopValues.FirstWalkspeed local koalaInv = game.ReplicatedStorage.HatValues.KoalaInventoryCount local koalaQue = game.ReplicatedStorage.HatValues.KoalaCount game.Players.PlayerAdded:Connect(function(player) local Data local success, err = pcall(function() Data = inventory:GetAsync(player.UserId.."_Data", values.Value) end) if success then print("loaded") values.Value = Data[1] koalaInv.Value = Data[2] koalaQue.Value = Data[3] else print("Error getting data") warn(err) end end) game.Players.PlayerRemoving:Connect(function(player) local toSave = {values.Value, koalaInv.Value, koalaQue.Value} local success, err = pcall(function() inventory:SetAsync(player.UserId.."_Data", toSave) end) if success then print ("data saved") else print("There was an error") warn(err) end end)
Hello.
Solution:
Use Player.PlayerGui
instead of game.StarterGui
as StarterGui
is the container for gui you start with. Gui's in StarterGui
get cloned into the player's PlayerGui
. PlayerGui
is the container for the actual gui itself.
Fixed Code:
local DataStoreService = game:GetService("DataStoreService") local inventory = DataStoreService:GetDataStore("Inventory") game.Players.PlayerAdded:Connect(function(player) local values = player:WaitForChild("PlayerGui").ShopValues.FirstWalkspeed local koalaInv = game.ReplicatedStorage.HatValues.KoalaInventoryCount local koalaQue = game.ReplicatedStorage.HatValues.KoalaCount local Data local success, err = pcall(function() Data = inventory:GetAsync(player.UserId.."_Data", values.Value) end) if success then print("loaded") values.Value = Data[1] koalaInv.Value = Data[2] koalaQue.Value = Data[3] else print("Error getting data") warn(err) end end) game.Players.PlayerRemoving:Connect(function(player) local values = player.PlayerGui.ShopValues.FirstWalkspeed local koalaInv = game.ReplicatedStorage.HatValues.KoalaInventoryCount local koalaQue = game.ReplicatedStorage.HatValues.KoalaCount local toSave = {values.Value, koalaInv.Value, koalaQue.Value} local success, err = pcall(function() inventory:SetAsync(player.UserId.."_Data", toSave) end) if success then print ("data saved") else print("There was an error") warn(err) end end)