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

What is wrong with this DataStore script?

Asked by 7 years ago
Edited 7 years ago

Hello, I wrote this script to test saving any leaderstats value and unfortunately it will not save the 'Schmeckles' value. Is anybody experienced enough with DataStore that they could point out what I've done wrong? I left the script in Server Script Storage.

local DataStore = game:GetService("DataStoreService"):GetDataStore('Currency')

game.Players.PlayerAdded:connect(function(player)

    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"
    local Schmeckles = Instance.new("IntValue", stats)
    Schmeckles.Name = "Schmeckles"

    local key = "player-"..player.userId

    local SavedValues = DataStore:GetAsync(key)

    if SavedValues then
        Schmeckles.Value = SavedValues[1]
    else
        DataStore:SetAsync(key, Schmeckles.Value)
    end
end)

game:BindToClose(function()

    game.Players.PlayerRemoving:connect(function(player)

    local key = "player-"..player.userId

    local Schmeckles = player.leaderstats.Schmeckles.Value
    DataStore:SetAsync(key, Schmeckles)


    end)
end)
0
try removing .Value from line 27 and adding it to Schmeckles on line 28 RobloxianDestory 262 — 7y
1
Why are you connecting the PlayerRemoving event when the server is shutting down? You are also saving the value but accessing it trying to load it as a table. User#5423 17 — 7y
0
Provide us with the error. LightModed 81 — 7y
0
It's all I could grasp from the wiki and numerous videos, thankyou for the comments I'll try them out. Light the point of this question was I could not see any visible error because it's a logic error. Samstergizmo 28 — 7y
0
kingdom could you elaborate on me loading the array? I can't see how I would solve this Samstergizmo 28 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

Try this? It waits 3 seconds to wait for the game to shut down if that doesn't work then tell me. Also waits for the data to load!

local DataStore = game:GetService("DataStoreService"):GetDataStore('Currency')
local RunService = game:GetService('RunService')

game.Players.PlayerAdded:connect(function(player)
if not RunService:IsStudio() then
Parent:WaitForDataReady()
end
    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"
    local Schmeckles = Instance.new("IntValue", stats)
    Schmeckles.Name = "Schmeckles"

    local key = "player-"..player.userId

    local SavedValues = DataStore:GetAsync(key)

    if SavedValues then
        Schmeckles.Value = SavedValues[1]
    else
        DataStore:SetAsync(key, Schmeckles.Value)
    end
end)


    game.Players.PlayerRemoving:connect(function(player)

    local key = "player-"..player.userId

    local Schmeckles = player.leaderstats.Schmeckles.Value
    DataStore:SetAsync(key, Schmeckles)


    end)

game.OnClose = function()
wait(3)
end


0
Hello, thanks for writing a script that was oddly generous. I'm afraid in line 06 you've left Parent undeclared, what did you intend on putting beforehand? Samstergizmo 28 — 7y
0
Oh sorry, Parent is suppose to be "player" lmao opss UltraUnitMode 419 — 7y
0
aha! Thanks, I'll let you know if it functions Samstergizmo 28 — 7y
0
Sorry! I'm afraid this one doesn't even let the instance 'Schmeckles' appear in leaderstats Samstergizmo 28 — 7y
View all comments (3 more)
0
Update: Managed to get it working again however will not save overall, Il test it later with two players when the game isn't closing Samstergizmo 28 — 7y
0
Oh, okay anymore questions? UltraUnitMode 419 — 7y
0
well, apart from the fact it just doesn't save - am I just putting it in the wrong place? Samstergizmo 28 — 7y
Ad

Answer this question