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

[Solved itself] Datastore problem with saving/loading, any ideas how to fix this?

Asked by 4 years ago
Edited 4 years ago

Alright, so I've recently found a problem with loading my data. If I rejoin the game with another player present, it will just load his size and his cash. I'm not 100% sure if the problem occurs when saving or loading.

local service = game:GetService("DataStoreService")
local datastore = service:GetDataStore("PlayerSize4")


game.Players.PlayerAdded:Connect(function(plr)





    local datatoload = {}
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local size = Instance.new("IntValue")
    size.Name = "Size"
    size.Parent = leaderstats


        local cash = Instance.new("IntValue")
    cash.Name = "Cash"
    cash.Parent = leaderstats


    local success,errorm = pcall(function()
         datatoload = datastore:GetAsync("Size-"..plr.UserId)
    end)

    if success then
if datatoload ~= nil then
        plr.leaderstats.Size.Value = datatoload[1]
            plr.leaderstats.Cash.Value = datatoload[2]
                print("Data loaded")
            end
    else
        error("Data could not be loaded.")
    end

end)






game.Players.PlayerRemoving:Connect(function(player)
    local datatosave = {}
    datatosave[1] = player.leaderstats.Size.Value
    datatosave[2] = player.leaderstats.Cash.Value
    local success,errorm = pcall(function()
    datastore:SetAsync("Size-"..player.UserId, datatosave)
    end)

    if success then
        print("Data saved")

    else
        error("An error occured while saving")
    end

end)

0
what is player2 on line 4? MrSuperKrut 167 — 4y
0
Just something I forgot to remove. But that's sadly not the problem. Crafter215 -5 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

Have you enabled access to API Services for your studio? Click Game Settings on home tab, then publish if it requers, click Security and enable this thing

0
Yes. I've already done that. Crafter215 -5 — 4y
0
The real problem is that if another player is online HIS data gets loaded instead of yours if you rejoin the game. It could also be that if you rejoin that somehow the others player data gets saved as yours etc. Crafter215 -5 — 4y
0
Ok let me test it, I want to locate this magic on my plc MrSuperKrut 167 — 4y
0
If you mean to load 2 players on roblox web not in the studio then I cant recreat this, I dont have second account, sorry. MrSuperKrut 167 — 4y
0
It’s fine. The problem fixed itself. Take a look at my answer down below. Thanks for helping me out though! Crafter215 -5 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Huh well, the problem seems to have solved itself. I've tried it out again and now everything's working smoothly as it should. Sorry to bother you!

Answer this question