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

I don't know how to data store, my script won't work. My variables are Fun and Money. Can you help?

Asked by 3 years ago
Edited 3 years ago

So i'm making a simulator game on roblox, and you gather fun and sell it to get money or collect coins to get money, but I dont know how to Data Store. My script for Money and Fun is in leaderstats, My script which is in ServerScriptService is here:

codeblock local DS = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
    wait()
    local plrkey = "id_"..plr.userid
    local save1 = plr.leaderstats.Fun
    local save2 = plr.leaderstats.Money

    local GetSaved = DS:GetAsync(plrkey)
    if GetSaved then
        save1.Value = GetSaved[1]
        save2.Value = GetSaved[2]
    else
        local NumberForSaving = {save1.Value, save2.Value}
        DS:GetAsync(plrkey, NumberForSaving)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    DS:SetAsync("id_"..plr.userid, {plr.leaderstats.Fun.Value, plr.leaderstats.Money.Value})
end)

I've tried it multiple times and it does nothing.

0
please add everything in a codeblock. Makes it more organized :) kkfilms_1 68 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

This might work:

local plrkey
local save1
local save2
local DS = game:GetService("DataStoreService"):GetDataStore("SaveData") game.Players.PlayerAdded:Connect(function(plr) 
    wait() 
    plrkey = "id_"..plr.UserId
    save1 = plr.leaderstats.Fun 
    save2 = plr.leaderstats.Money
    local GetSaved = pcall(function()
        DS:GetAsync(plrkey)
    end)
    if GetSaved then
            save1.Value = GetSaved[1]
            save2.Value = GetSaved[2]
    end
end)
game.Players.PlayerRemoving:Connect(function(plr) 
    NumberForSaving = {save1.Value, save2.Value}
    local Saved = pcall(function()
        DS:SetAsync(plrkey,NumberForSaving) 
    end)
end)
0
it did not work. I'm putting it under the right thing right? I'm named the script SaveData and I put it under ServerScriptService? kylerSilverdiamond 5 — 3y
Ad

Answer this question