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