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

Datastore not saving the time?

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

0

pretty sure if you just remove the {} brackets it will work or just use local PlayTime = save[1]

0

Thanks I'll try it

0

ServerScriptService.Leaderstats.CardTimes:10: attempt to index local 'save' (a number value) @abnotaddable

0

have you tried the second option?

0

keeping the brackets

0

but putting save[1]

0

Yeah

0

I'm going to try and JSONEncode the table

0

hmm that didn't work any other ideas? @abnotaddable

0

I'll look into it

0

alright thanks

1 answer

✓ Accepted answer

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

3 comments

0

Alright let me test it :)

0

Thanks it worked :)

0

:)

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