I have a data store script that saves data, basically the in-game seconds. They increase by 1 after second and I want to save them.
My whole script:
--// Variables, Services and functions
local dataStore = game:GetService("DataStoreService"):GetDataStore("BucksDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local gameSeconds = ReplicatedStorage:WaitForChild("GameSeconds")
local playersLeft = 0
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
playersLeft = playersLeft + 1
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local playerName = Instance.new("StringValue")
playerName.Name = "Name"
playerName.Value = player.Name
playerName.Parent = leaderstats
local Teir = Instance.new("StringValue")
Teir.Name = "Teir"
Teir.Value = "o"
Teir.Parent = leaderstats
local timeSpent = Instance.new("IntValue")
timeSpent.Name = "Game Time"
timeSpent.Value = gameSeconds.Value
timeSpent.Parent = leaderstats
gameSeconds.Changed:Connect(function()
player.leaderstats["Game Time"].Value = gameSeconds.Value
end)
-- Data stores
local player_data
pcall(function()
player_data = dataStore:GetAsync(player.UserId.."-Time")
end)
if player_data ~= nil then
timeSpent.Value = player_data
else
-- New player
timeSpent.Value = 0
end
end)
local bindableEvent = Instance.new("BindableEvent")
Players.PlayerRemoving:Connect(function(player)
pcall(function()
dataStore:SetAsync(player.UserId.."-Time",player.leaderstats["Game Time"].Value)
print("Saved player's data")
playersLeft = playersLeft -1
bindableEvent:Fire()
end)
end)
game:BindToClose(function()
while playersLeft > 0 do
bindableEvent.Event:Wait()
end
end)
It even prints Data saved and API services are turned on as well as I test this in the actual game. What could be the issue?