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)

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