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

Why is this not Saving or Loading?

Asked by 9 years ago

So, this script is supposed to Save and Load the time spent in-game by the people above or that rank in the group, but it prints this error after it has counted 1 minute, can anyone help me understand why it thinks it is missing or nil?

-- Argument 2 missing or nil

-- Script 'ServerScriptService.StaffTime.StaffTimeCounter', line 74 - global TimeCount

-- Script 'ServerScriptService.StaffTime.StaffTimeCounter', line 94

-- Stack end

The script is:

-- Primary Variables:
local DataStore     = game:GetService('DataStoreService'):GetDataStore('StaffTime');

local SP            = script.Parent;

local Users         = SP:WaitForChild('Users');
local Default       = Users:WaitForChild('Default');

local Settings      = require(SP:WaitForChild('Settings'));
local GroupId       = Settings.GroupId;
local StaffRank     = Settings.StaffRank;
local ViewerRank    = Settings.ViewerRank;

-- Primary Functions:
function CreatePlayerTime(Player)
    local PlayerTime    = Default:Clone()
    PlayerTime.Name     = Player.Name
    PlayerTime.Parent   = Users
    local TimeInstances = {
        Days    = PlayerTime:WaitForChild('Days');
        Hours   = PlayerTime:WaitForChild('Hours');
        Minutes = PlayerTime:WaitForChild('Minutes');
        Seconds = PlayerTime:WaitForChild('Seconds')
    }
    return TimeInstances
end

function SaveTime(Player, PlayerTime, OldValue)
    local SaveEnsure = pcall(function()
        if (type(PlayerTime) == ('table')) then
            local PlayerTimeValues = {
                Days    = PlayerTime.Days.Value;
                Hours   = PlayerTime.Hours.Value;
                Minutes = PlayerTime.Minutes.Value;
                Seconds = PlayerTime.Seconds.Value;
            }
            return PlayerTimeValues
        end
    end)
    if (not SaveEnsure) then
        wait()
    end
end

function LoadTime(Player, PlayerTime)
    local LoadEnsure = pcall(function()
        if (type(PlayerTime) == ('table')) then
            local Key = 'Id_'..Player.userId
            if (DataStore:GetAsync(Key)) then
                PlayerTime.Days.Value = DataStore:GetAsync(Key).Days
                PlayerTime.Hours.Value = DataStore:GetAsync(Key).Hours
                PlayerTime.Minutes.Value = DataStore:GetAsync(Key).Minutes
                PlayerTime.Seconds.Value = DataStore:GetAsync(Key).Seconds
            elseif (not DataStore:GetAsync(Key)) then
                return false
            end
        end
    end)
    if (not LoadEnsure) then
        wait()
    end
end

function TimeCount(Player, PlayerTime)
    if (type(PlayerTime) == ('table')) then
        local Key = 'Id_'..Player.userId
        while wait(1) do
            if (PlayerTime.Seconds.Value < 59) then
                PlayerTime.Seconds.Value = PlayerTime.Seconds.Value + 1
            elseif (PlayerTime.Seconds.Value >= 59) then
                PlayerTime.Seconds.Value = 0
                if (PlayerTime.Minutes.Value < 59) then
                    PlayerTime.Minutes.Value = PlayerTime.Minutes.Value + 1
                    DataStore:UpdateAsync(Key, SaveTime(Player, PlayerTime)) -- Line 74
                elseif (PlayerTime.Minutes.Value >= 59) then
                    PlayerTime.Minutes.Value = 0
                    if (PlayerTime.Hours.Value < 23) then
                        PlayerTime.Hours.Value = PlayerTime.Hours.Value + 1
                    elseif (PlayerTime.Hours.Value >= 23) then
                        PlayerTime.Hours.Value = 0
                        PlayerTime.Days.Value = PlayerTime.Days.Value + 1
                    end
                end
            end
        end
    end
end

-- Primary Code:
game.Players.PlayerAdded:connect(function(Player)
    if (Player:GetRankInGroup(GroupId) >= StaffRank) then
        local PlayerTime = CreatePlayerTime(Player)
        LoadTime(Player, PlayerTime)
        TimeCount(Player, PlayerTime) -- Line 94
    end 
end)

game.Players.PlayerRemoving:connect(function(Player)
    local PlayerTime = Users:FindFirstChild(Player.Name)
    if (PlayerTime) then
        PlayerTime:remove()
    end
end)
1
You are caling the function therefore passing whatever that function returns to UpdateAsync. Create an annymous function to call SaveTime. Also to save a table you have to encode it in  JSON. NotsoPenguin 705 — 9y
0
Thank you, it works fine now! Shrekerly 70 — 9y

Answer this question