Im not good ate DataStorage, and a good guys tryied to help me, but im still with this problem, i know it is my fault, and i cant see where is the problem
Save : this is in the script in the save button
local DataStoreService = game:GetService("DataStoreService") local playerExperienceStore = DataStoreService:GetDataStore("PlayerDS") local data = {} for k,v in pairs(stats:GetChildren()) do data[k] = v end PlayerDS:SetAsync( Player.UserId, data)
Load : this is in the script in the load Button
local DataStoreService = game:GetService("DataStoreService") local playerExperienceStore = DataStoreService:GetDataStore("PlayerDS") local PlayerData = PlayerDS:GetAsync( Player.UserId) for k,v in pairs(PlayerData) do stats[k].Value = v end
Save V2
local DataStoreService = game:GetService("DataStoreService") local playerExperienceStore = DataStoreService:GetDataStore("PlayerDS") function Click(mouse) local data = {} for k,v in pairs(stats:GetChildren()) do data[k] = v end end PlayerDS:SetAsync( Player.UserId, data) script.Parent.MouseButton1Down:connect(Click)
Load v2
local DataStoreService = game:GetService("DataStoreService") local playerExperienceStore = DataStoreService:GetDataStore("PlayerDS") function Click(mouse) local PlayerData = PlayerDS:GetAsync( Player.UserId) for k,v in pairs(PlayerData) do stats[k].Value = v end end script.Parent.MouseButton1Down:connect(Click)
There are a couple problems with the two scripts. First in your save script, you must save the value of the instance and not the instance itself.
Additionally in your load script, you must check that the data exists for the player because the player might be joining for the first time.
Finally, it is always best practice to use pcall()
on your functions that are part of data store.
Save Script:
local DataStoreService = game:GetService("DataStoreService") local playerExperienceStore = DataStoreService:GetDataStore("PlayerDS") function Click (mouse) local data = {} for k,v in pairs(stats:GetChildren()) do data[k] = v.Value end local success , message = pcall(function () PlayerDS:SetAsync(Player.UserId, data) end) if not success then --Error happened with data store --Handle error here end end script.Parent.MouseButton1Down:Connect(Click)
Load Script:
local DataStoreService = game:GetService("DataStoreService") local playerExperienceStore = DataStoreService:GetDataStore("PlayerDS") function Click (mouse) local success , PlayerData = pcall(function () return PlayerDS:GetAsync(Player.UserId) end) if success then if PlayerData then for k,v in pairs(PlayerData) do stats[k].Value = v end else --Player does not have any data saved --Best to set stats to default values here end else --Error happened with data store --Handle error here end end script.Parent.MouseButton1Down:connect(Click)
To use datastore across multiple places, you can either use a GLOBAL DATASTORE, or you can add your PLACES into a UNIVERSE.