Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't my datastore work correctly?

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)

2 comments

1

You won't see anything happen if Player:WaitForChild("stats"):WaitForChild("Level") never completes.

0

ah

1 answer

✓ Accepted answer

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))

This discussion is preserved as it was. New answers, comments, and votes are closed.