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

PlayerData a nil value, how to set my DataStore?

Asked by 5 years ago
Edited 5 years ago

ServerScriptService.Script:44: attempt to perform arithmetic on local 'playerData' (a nil value)?

Why my PlayerData return nil? I would like make a Daily Reward system with a UI countdown. This is my first script (ServerScript) SOURCE. Dont close my question for that please just awnser...

-< Services
local PlayerService = game:GetService("Players")
local RStorageService = game:GetService('ReplicatedStorage')
local DataStoreService = game:GetService("DataStoreService")

--< Variables
local DailyRewards = DataStoreService:GetDataStore("dailyRewards")

local RewardTime = 24 -- one day
local RewardAmmount = 50 --they get 50 money each day

local SessionData = {}

--< Event
PlayerService.PlayerAdded:Connect(function(player)
    local ls = Instance.new("Folder")
    ls.Name = "leaderstats"
    ls.Parent = player

    SessionData[player] = {}
    SessionData[player]["Cash"] = 100

    local money = Instance.new("IntValue", ls)
    money.Name = "Cash"
    money.Value = SessionData[player]["Cash"]

    local playerData = nil

    pcall(function() -- no errors pls
        playerData = DailyRewards:GetAsync("player_" .. player.UserId)
    end)

    if playerData then
        if ((tick() - playerData) / (60 * 60)) >= RewardTime then
            money.Value = playerData + RewardAmmount
            DailyRewards:SetAsync(player.UserId, tick())
        else
            money.Value = playerData
        end
    else
        DailyRewards:SetAsync(player.UserId, tick())
        playerData = DailyRewards:GetAsync("player_" .. player.UserId)
    end
    print(require(script.Parent.ModuleScript):ConvertTime((tick() - playerData)))
end

And this is my module script for convert "example" 120s to 00:02:00 for return this to a playerUI from RemoteEvent. This script is a FreeModel find in the ToolBox. So its "FREE" model. Dont close my question for this. I just like create game and powering my imagination.

local module = {}

local Type = 0

function module:ConvertTime(t)
    -- Variables

    -- Sec, Min, and Hour equations I actually got off of Google. Everything else was written by me
    local sec = math.floor((t%60))
    local min = math.floor((t/60)%60)
    local hour = math.floor((t/3600)%24)

    -- Set time
    sec = tostring((sec < 10 and "0" or "") .. sec)
    min = tostring((min < 10 and "0" or "") .. min)
    -- Ternary operators FTW. Helps get rid of 'if then' stuff. Actually learned this off of Java programming:  z = (x == y ? "Yes" : "No")
    hour = ((Type == 1 and hour < 10 and "0" or "") .. tostring(Type == 0 and (hour == 0 and 12 or hour > 12 and (hour-12) or hour) or hour))

    -- Colon, (space if 12 hr clock)
    local c, s = ":",(Type == 0 and " " or "")

    return (hour .. c .. min .. (c .. sec or "") .. s)
end

return module

Thanks for answers, i really would like make a good Daily Reward system with UI countdown, but i dont know how, but with your answers i will be able.

Sorry for my grammar errors i not speak english so is hard for me learn with the Wiki or others source...

0
I know how to script with Lua, but not all... NiniBlackJackQc 1562 — 5y
0
You are setting then getting the same key very quickly with no error handling on lines 41, 42 which can cause an error on line 44. Just set playerData = tick() instead of saving the data. User#5423 17 — 5y

Answer this question