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

Script Not Getting the Correct Value From Variable?

Asked by 5 years ago
Edited 5 years ago

I originally thought this was an issue with datastore, but upon further examination I determined that the data was saving, but the Rubles value from the player removing function is getting the wrong Rubles Value. For instance, when I set the Rubles value to 25, and then I click stop, even though the Rubles Variable is 25, the script still outputs 10 when printing savetable. The reason I know this to be true is because I edited the line "local Savetable = {plr.leaderstat.Rubles.Value}" to "local Savetable = {plr.leaderstat.Rubles.Value+2}". And next time I load the game, I have 12. So I'm wondering why incorrect data is being retrieved here.

local DataStoreService = game:GetService('DataStoreService')
local moneyStore = DataStoreService:GetDataStore('Djmaster444TestStore03')
game.Players.PlayerAdded:connect(function(plr)
    plr:WaitForChild(plr.Backpack.Name)
    -- Define variables
    local uniquekey = 'id-'..plr.userId
    local leaderstat = Instance.new('NumberValue', plr)
    local savevalue =  Instance.new('NumberValue')
    leaderstat.Name = 'leaderstat'
    savevalue.Parent = leaderstat
    savevalue.Name = 'Rubles'
    savevalue.Value = 10.00

    -- GetAsync
    local GetSaved = moneyStore:GetAsync(uniquekey)
    print(GetSaved[1])
    if GetSaved then
        savevalue.Value = GetSaved[1]
    else
        local NumbersForSaving = {savevalue.Value}
        moneyStore:SetAsync(uniquekey, NumbersForSaving)
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    saveData(plr)
end)

function saveData(plr)
    local uniquekey = 'id-'..plr.userId
    local Savetable = {plr.leaderstat.Rubles.Value}
    moneyStore:SetAsync(uniquekey, Savetable)
end

1 answer

Log in to vote
0
Answered by 5 years ago

you could try my datastore model: https://www.roblox.com/library/2790189297/Datastore Heres how it works

--You must publish the game for it to work
local Mod = require(game.ServerStorage.StatsHandler)
Mod:GetFolderName("leaderstats")
Mod:GetStarterData({Rubles = 10})

game.Players.PlayerAdded:Connect(function(plr)
    Mod:LoadStarterData(plr)
    Mod:LoadData(plr)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    Mod:SaveData(plr)
end)
0
I appreciate the help, but I'd also like to know the data is not saving. Djmaster444 5 — 5y
0
You can go check my module to check out how it works Dalbertjdplayz 37 — 5y
0
or check out here https://pastebin.com/VxSNp0cV It has pcalls because there might be a error while loading/saving data so the pcall wont error and the script wont break down Dalbertjdplayz 37 — 5y
0
It's not datastore. I did some diagnosing, and I've realized that savetable is not reading the the "Rubles" value correctly. I'm going to edit the question to accommodate this. Djmaster444 5 — 5y
0
Oh ok Dalbertjdplayz 37 — 5y
Ad

Answer this question