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

DataStore not working? No error.. Kind of copied a video but changed it up.

Asked by 5 years ago

Can anyone tell me why this DataStore is not working..

local DataStoreService = game:GetService("DataStoreService"):GetDataStore("Hello")
game.Players.PlayerAdded:Connect(function(plr)

    local uniquekey = "id-"..plr.userId
    local leaderstats = Instance.new("IntValue",plr)
    local savevalue = Instance.new("IntValue")
    local savevalue2 = Instance.new("IntValue")
    local savevalue3 = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    savevalue.Parent = leaderstats
    savevalue.Name = "Money"
    savevalue2.Parent = leaderstats
    savevalue2.Name = "Subscribers"
    savevalue3.Parent = leaderstats
    savevalue3.Name = "Followers"

    local GetSaved = DataStoreService:GetAsync(uniquekey)
    if GetSaved then
        savevalue.Value = GetSaved[1]
        savevalue2.Value = GetSaved[2]
        savevalue3.Value = GetSaved[3]
    else
        local NumbersForSaving = {savevalue.Value, savevalue2.Value, savevalue3.Value}
        DataStoreService:SetAsync(uniquekey, NumbersForSaving)
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local uniquekey = "id-"..plr.userId
    local Savetable = {plr.leaderstats.Cash.Value, plr.leaderstats.Subscribers.Value, plr.leaderstats.Followers.Value}
    DataStoreService:SetAsync(uniquekey, Savetable)



end)


1 answer

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

The problem is that you are calling SetAsync in the else statement in the playeradded event. You need to realise that that method yields the script until the data is found and considering that your providing the else statement with the fact it has no data to save; it will yield it forever. Here is the fix:

--MAKE SURE IT IS A SERVERSCRIPT IN SERVERSCRIPTSERVICE
local DataStoreService = game:GetService("DataStoreService"):GetDataStore("Hello")
game.Players.PlayerAdded:Connect(function(plr)

    local uniquekey = "id-"..plr.userId
    local leaderstats = Instance.new("IntValue",plr)
    local savevalue = Instance.new("IntValue")
    local savevalue2 = Instance.new("IntValue")
    local savevalue3 = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    savevalue.Parent = leaderstats
    savevalue.Name = "Money"
    savevalue2.Parent = leaderstats
    savevalue2.Name = "Subscribers"
    savevalue3.Parent = leaderstats
    savevalue3.Name = "Followers"

    local GetSaved = DataStoreService:GetAsync(uniquekey) or {} --Checks if there is data to load
    if GetSaved then
        savevalue.Value = GetSaved[1] or 0 --If there is no data then just give it a default of 0.
        savevalue2.Value = GetSaved[2] or 0 --If there is no data then just give it a default of 0. 
        savevalue3.Value = GetSaved[3] or 0 --If there is no data then just give it a default of 0.
    --Since we have already certified any errors appearing if the data is nil, there is no need for the else statement.
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local uniquekey = "id-"..plr.userId
    local Savetable = {plr.leaderstats.Cash.Value, plr.leaderstats.Subscribers.Value,                
    plr.leaderstats.Followers.Value}
    DataStoreService:SetAsync(uniquekey, Savetable)
end)
0
@MaximumOverdriven That dosen't work. CoreMcNugget 15 — 5y
0
It dosen't save the data CoreMcNugget 15 — 5y
0
yes it does MaximumOverdriven 152 — 5y
0
When the player leaves itsaves the data MaximumOverdriven 152 — 5y
View all comments (5 more)
0
let me explain to you, getasync loads the data and if there is no data we getan empty array which represents {}, next we load the values and if there is no value, we make 0 our default; with the or . You shouldn't be setting the async in the playeradded event MaximumOverdriven 152 — 5y
0
I KNOW WHAT THE PROBLEM IS MaximumOverdriven 152 — 5y
0
Bro, I went into developer console gave me subs etc, I see it on the leaderboard and when I rejoin its gone.. CoreMcNugget 15 — 5y
0
in the player removing event, you did plr.leaderstats.Cash.Value; when it should be plr.leaderstats.Money.Value MaximumOverdriven 152 — 5y
0
accept answer if im right :) MaximumOverdriven 152 — 5y
Ad

Answer this question