Ad
Log in to vote
0

Save and Load in the same game but different places isnt working, why?

Asked by 11 months ago
Edited 11 months ago

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)

2 answers

Log in to vote
0
Answered by
RayCurse 1225 Moderation Voter
11 months ago
Edited 11 months ago

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)
0
don't worked :c darkzerobits 37 — 11mo
0
still not working ;~; darkzerobits 37 — 11mo
0
idk about save, but when i go to the other place, and load, the stats still 0 ( default ), i dont added scripts in the green positions yet darkzerobits 37 — 11mo
Ad
Log in to vote
0
Answered by
H4X0MSYT 536 Moderation Voter
11 months ago

To use datastore across multiple places, you can either use a GLOBAL DATASTORE, or you can add your PLACES into a UNIVERSE.

0
they are already in the same universe darkzerobits 37 — 11mo

Answer this question