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

Datastore not saving the time?

Asked by 7 years ago
Edited 7 years ago

I'm trying to get the amount of playtime each player has done and save that to a datastore so next time they leave I can add the playtime onto it. No errors it just isn't saving... Code:

local DataStore = game:GetService('DataStoreService'):GetDataStore(Datastore Name)

game.Players.PlayerAdded:connect(function(plr)
    wait(1)
    local key = "Save_".. plr.UserId
    local save = DataStore:GetAsync(key)

    if save then
        local PlayTime = save
        print(PlayTime)        

        local info = Instance.new('Folder', plr)
        info.Name = 'Info'
        local JoinTime = Instance.new('IntValue', info)
        JoinTime.Name = "JoinTime"
        local playTime = Instance.new('IntValue', info)
        playTime.Name = "PlayTime"

        playTime.Value = PlayTime
        JoinTime.Value = tick()
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local key = "Save_".. plr.UserId
    local JoinTime = plr:FindFirstChild('Info').JoinTime
    local PlayTime = plr:FindFirstChild('Info').PlayTime
    local leavetime = tick()    
    local NewPlayTime = leavetime - JoinTime.Value
    NewPlayTime = math.floor(NewPlayTime + 0.5) 
    NewPlayTime = PlayTime.Value + NewPlayTime

    DataStore:SetAsync(key, {NewPlayTime})
end)

edit: New code edits

local DataStore = game:GetService('DataStoreService'):GetDataStore(DS Name)
local FirstPlay = Instance.new('RemoteEvent', game.ReplicatedStorage.Events)
HttpService = game:GetService("HttpService")

game.Players.PlayerAdded:connect(function(plr)
    wait(1)
    local key = "Save_".. plr.UserId
    local save = DataStore:GetAsync(key)

    if save then

        local PlayTime = save
        print(PlayTime)        

        local info = Instance.new('Folder', plr)
        info.Name = 'Info'
        local JoinTime = Instance.new('NumberValue', info)
        JoinTime.Name = "JoinTime"
        local playTime = Instance.new('IntValue', info)
        playTime.Name = "PlayTime"

        playTime.Value = PlayTime
        JoinTime.Value = tick()
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local key = "Save_".. plr.UserId
    local JoinTime = plr:FindFirstChild('Info').JoinTime
    local PlayTime = plr:FindFirstChild('Info').PlayTime
    local leavetime = tick()    
    local NewPlayTime = leavetime - JoinTime.Value
    NewPlayTime = math.floor(NewPlayTime + 0.5) 
    print(NewPlayTime)
    NewPlayTime = PlayTime.Value + NewPlayTime
    NewPlayTime = tonumber(NewPlayTime)

    DataStore:SetAsync(key, NewPlayTime)
end)
0
pretty sure if you just remove the {} brackets it will work or just use local PlayTime = save[1] abnotaddable 920 — 7y
0
Thanks I'll try it DevOverr 15 — 7y
0
ServerScriptService.Leaderstats.CardTimes:10: attempt to index local 'save' (a number value) @abnotaddable DevOverr 15 — 7y
0
have you tried the second option? abnotaddable 920 — 7y
View all comments (7 more)
0
keeping the brackets abnotaddable 920 — 7y
0
but putting save[1] abnotaddable 920 — 7y
0
Yeah DevOverr 15 — 7y
0
I'm going to try and JSONEncode the table DevOverr 15 — 7y
0
hmm that didn't work any other ideas? @abnotaddable DevOverr 15 — 7y
0
I'll look into it abnotaddable 920 — 7y
0
alright thanks DevOverr 15 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

A module script in ServerScriptService:

local module = {}

local DSS = game:GetService("DataStoreService")
local PlayTimeDataStore = DSS:GetDataStore("PlayTime")

game.Players.PlayerAdded:Connect(function(Player)
    wait(1)
    local key = "PlayTimePlayer_" .. Player.UserId
    local SavedData = PlayTimeDataStore:GetAsync(key)
    local INew = Instance.new -- Cache (makes game faster)

    local Info = INew("Folder")
    Info.Name = "Info"
    Info.Parent = Player --you should always parent it last (Read : https://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296)

    local JoinTime = INew("IntValue")
    JoinTime.Name = "JoinTime"
    JoinTime.Parent = Info

    local PlayTime = INew("IntValue")
    PlayTime.Name = "PlayTime"
    PlayTime.Parent = Info

    if SavedData then --they have data...
        local TimePlayed = SavedData[1]
        print(TimePlayed)
        PlayTime.Value = TimePlayed
        JoinTime.Value = math.floor(tick())
    else --there is no data...
        local SavedData = {}
        SavedData[1] = 0
        PlayTime.Value = 0
        JoinTime.Value = math.floor(tick())
        PlayTimeDataStore:SetAsync(key, SavedData[1])
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local key = "PlayTimePlayer_" .. Player.UserId
    local JoinTime = Player:FindFirstChild("Info").JoinTime
    local PlayTime = Player:FindFirstChild("Info").PlayTime
    local LeaveTime = math.floor(tick())
    local PlayTimeLength = LeaveTime - JoinTime.Value
    print("Todays Play Time for " .. Player.Name .. " : " ..PlayTimeLength)
    PlayTime.Value = PlayTime.Value + PlayTimeLength
    print("Total Play Time for " .. Player.Name .. " : " ..PlayTime.Value)
    PlayTimeDataStore:SetAsync(key, {PlayTime.Value}) --Must be saved as .Value
end)

return module

A script inside of the Module Script

require(script.Parent)

Do not change anything here. It has been tested and works successfully. Accept if it helped solve your question :)

0
Alright let me test it :) DevOverr 15 — 7y
0
Thanks it worked :) DevOverr 15 — 7y
0
:) abnotaddable 920 — 7y
Ad

Answer this question