Ad
Log in to vote
0

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

Asked by 4 days ago
Edited 4 days 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)

Hi.

Looks like you're using an ad blocker.

That's fine...

No, it really is.

Just, I mean, we put a lot of work into this site, you know?

It would be really really appreciated if you would turn off your ad blocker for our website...

We tried really hard to make our ads as unobtrusive as possible.

If you really hate ads, would you consider a $2 donation via Patreon?

Here's the link.

We love you. We hope you love us too.

.

..

...

Now back to your regularly scheduled Scripting Helpers....

...

..

.

2 answers

Log in to vote
0
Answered by
Y0_dude 268
4 days ago
Edited 4 days 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 14 — 4d
0
still not working ;~; darkzerobits 14 — 4d
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 14 — 4d
Ad
Log in to vote
0
Answered by
H4X0MSYT 193
4 days 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 14 — 3d

Answer this question



Ad