Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My datastore wont Save/Load, I have tried many things. How can I solve this problem?

Asked by
Leutze 2
1 year ago

I have been trying different variations of code and viewed many hours on forums and Youtube videos on data stores and still am confused. I have done 2 datastores for cloning and level-stats and they work flawless, not sure why this is different.

I want the IronSword.Value to be saved and loaded on join and leave... The pcall prints that it's saving and loading but the value goes back to 0.


local DataStoreService = game:GetService("DataStoreService") local Data = DataStoreService:GetDataStore("Stats") local Store = DataStoreService:GetDataStore("StoreItems") local function onPlayerJoin(player) local leaderstats = Instance.new("IntValue") leaderstats.Name = "Storage" leaderstats.Parent = player local inv = Instance.new("Folder") inv.Name = "Inventory" inv.Parent = leaderstats end ----------------------------------------------------------------------------- local weapons = game.ReplicatedStorage.Weapons game.Players.PlayerAdded:Connect(function(plr) local saveditems = plr.Storage.Inventory:WaitForChild("IronSword").Value local success, err = pcall(function() Store:GetAsync(plr.UserId.."-Values", saveditems) end) print(saveditems) if success then print("value loaded") else print("value failed to load") end while true do local saveditems = plr.Storage.Inventory:WaitForChild("IronSword").Value wait(10) print(saveditems) Data:SetAsync(plr.UserId.."-Values", saveditems) end end) ----------------------------------------------------------------------------- game.Players.PlayerRemoving:Connect(function(plr) local savedval = plr.Storage.Inventory:WaitForChild("IronSword").Value local success, err = pcall(function() Store:SetAsync(plr.UserId.."-Values", savedval) end) print(savedval) if success then print("value saved") else print("value failed to load") warn(err) end end) ----------------------------------------------------------------------------- game.Players.PlayerAdded:Connect(onPlayerJoin) game:BindToClose(function() wait(2) end)

I want the IronSword.Value to be saved and loaded on join and leave... The pcall prints that it's saving and loading but the value goes back to 0.

I have been trying different variations of code and many forums and Youtube videos on data stores and still am confused.

Imgur

^ this is the item and location I want to save/load the value for.

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

Store:GetAsync that you are using is incorrect because the err variable isn't actually used for reading the error message but used for reading the value obtained from Store:GetAsync.

Please note that

  1. SetAsync returns success boolean, and error message.

  2. GetAsync returns success boolean, and the value (not an error message).

Additionally, you didn't set the ironSword value after reading the value meaning the ironSword value is still 0 because it remains unchanged by the code.

Here's the fix for line 32

local saveditems = plr.Storage.Inventory:WaitForChild("IronSword")
local success, data  = pcall(function()
    return Store:GetAsync(plr.UserId.."-Values")
end)
saveditems.Value = data
  • GetAsync doesn't accept additional parameters for setting variables meaning Store:GetAsync(plr.UserId.."-Values", saveditems) will not set the value of savedItems variable.

  • plr.Storage.Inventory:WaitForChild("IronSword").Value returns the value of IronSword itself which is for reading value but not for setting value. You have to use plr.Storage.Inventory:WaitForChild("IronSword") to return the instance and use savedItems.Value to actually set the value.

If you find this helpful or working then please accept the answer.

0
That makes so much sense, I was not able to find anything about that on my own. Thank you man <3 Leutze 2 — 1y
Ad

Answer this question