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

Why won't my datastore work correctly?

Asked by
Prioxis 673 Moderation Voter
9 years ago

I don't see any errors when ever I play my game through the site but it still doesn't work

Here's my script :

local DataStore = Game:GetService("DataStoreService"):GetDataStore("Level")

local Requests = 0
local Max  = 60

function Good()
    return Requests <= Max
end

function UpdateMax()
    Max = 60 + 10 * #Game.Players:GetPlayers()
end


Game.Players.PlayerAdded:connect(function(Player)
    local Stat = Player:WaitForChild("stats"):WaitForChild("Level")
    Stat.Value = DataStore:GetAsync(Player.userId)
    Stat.Changed:connect(function()
        if Good() then
            DataStore:SetAsync(Player.userId,Stat.Value)
            Requests = Requests + 1
        end
    end)
end)
Game.Players.PlayerRemoving:connect(function()
    UpdateMax()
end)

Spawn(function()
    while wait(60) do
        Requests = 0
    end
end)

1
You won't see anything happen if Player:WaitForChild("stats"):WaitForChild("Level") never completes. 1waffle1 2908 — 9y
0
ah Prioxis 673 — 9y

1 answer

Log in to vote
0
Answered by 8 years ago

You never call UpdateMax() when a new player joins.

Secondly, I'm not entirely sure about Spawn(). I never use it, so I wouldn't trust it. I would suggest just using a normal coroutine.

Also, when you call :GetAsync() you never increase requests. Get requests DO use part of your allowed request quota.

local DataStore = Game:GetService("DataStoreService"):GetDataStore("Level")

local Requests = 0
local Max  = 60

function Good()
    return Requests <= Max
end

function UpdateMax()
    Max = 60 + 10 * #Game.Players:GetPlayers()
end


Game.Players.PlayerAdded:connect(function(Player)
    UpdateMax()
    local Stat = Player:WaitForChild("stats"):WaitForChild("Level")
    Stat.Value = DataStore:GetAsync(Player.userId)
    Requests = Requests + 1;
    Stat.Changed:connect(function()
        if Good() then
            DataStore:SetAsync(Player.userId,Stat.Value)
            Requests = Requests + 1
        end
    end)
end)
Game.Players.PlayerRemoving:connect(function()
    UpdateMax()
end)

coroutine.resume(coroutine.create(function()
    while wait(60) do
        Requests = 0
    end
end))
Ad

Answer this question