I made a leaderstat called “Time” that uses datastore to save, every second the Time decreases by 1 and I want the player to start with 900 seconds (15 minutes), but when a new player joins the game, it sets as 0. I tried resetting my data using a datastore management plugin, but it won’t work. Can anyone help?
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local TimeDataStore = DataStoreService:GetDataStore("TimeDataStore")
local function playerAdded(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local time = Instance.new("IntValue")
time.Name = "Time"
time.Value = 900 --the issue
time.Parent = leaderstats
if time.Value > 0 then
time.Changed:Connect(function()
if time.Value < 1 then
time.Value = 0
end
end)
end
local success, value = pcall(function()
return TimeDataStore:GetAsync(tostring(plr.UserId))
end)
if success and value then
time.Value = value
else
time.Value = 0
end
time.Parent = leaderstats
while true do
wait(1)
plr.leaderstats.Time.Value = plr.leaderstats.Time.Value - 1
end
end
local function playerLeaving(plr)
local success, err = pcall(function()
TimeDataStore:SetAsync(tostring(plr.UserId), plr.leaderstats.Time.Value)
end)
if not success then
warn("Failed to save Timer data for player " .. plr.Name .. ": " .. err)
end
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerLeaving)
game:BindToClose(function()
task.wait(10)
end)
Im not sure why it doesnt work however when i do stuff like this I use locals for start amounts so try doing a local like below:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local TimeDataStore = DataStoreService:GetDataStore("TimeDataStore")
local StartTime = 900
local function playerAdded(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local time = Instance.new("IntValue")
time.Name = "Time"
time.Value = StartTime
time.Parent = leaderstats
if time.Value > 0 then
time.Changed:Connect(function()
if time.Value < 1 then
time.Value = 0
end
end)
end
local success, value = pcall(function()
return TimeDataStore:GetAsync(tostring(plr.UserId))
end)
if success and value then
time.Value = value
else
time.Value = 0
end
time.Parent = leaderstats
while true do
wait(1)
plr.leaderstats.Time.Value = plr.leaderstats.Time.Value - 1
end
end
local function playerLeaving(plr)
local success, err = pcall(function()
TimeDataStore:SetAsync(tostring(plr.UserId), plr.leaderstats.Time.Value)
end)
if not success then
warn("Failed to save Timer data for player " .. plr.Name .. ": " .. err)
end
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerLeaving)
game:BindToClose(function()
task.wait(10)
end)
Edit: Pretty sure your setting it to 0, under the if success and value then, if statement:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local TimeDataStore = DataStoreService:GetDataStore("TimeDataStore")
local function playerAdded(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local time = Instance.new("IntValue")
time.Name = "Time"
time.Value = 900 --the issue
time.Parent = leaderstats
if time.Value > 0 then
time.Changed:Connect(function()
if time.Value < 1 then
time.Value = 0
end
end)
end
local success, value = pcall(function()
return TimeDataStore:GetAsync(tostring(plr.UserId))
end)
if success and value then
time.Value = value
else
time.Value = 900
end
time.Parent = leaderstats
while true do
wait(1)
plr.leaderstats.Time.Value = plr.leaderstats.Time.Value - 1
end
end
local function playerLeaving(plr)
local success, err = pcall(function()
TimeDataStore:SetAsync(tostring(plr.UserId), plr.leaderstats.Time.Value)
end)
if not success then
warn("Failed to save Timer data for player " .. plr.Name .. ": " .. err)
end
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerLeaving)
game:BindToClose(function()
task.wait(10)
end)
When I test the code I provided, for me it seems to work and sets the time.Value to 900, when the player first initially joins the game which is what it should be doing? Is the script inside ServerScriptService?
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local TimeDataStore = DataStoreService:GetDataStore("TimeDataStore")
local function playerAdded(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local time = Instance.new("IntValue")
time.Name = "Time"
time.Value = 900 --the issue
time.Parent = leaderstats
if time.Value > 0 then
time.Changed:Connect(function()
if time.Value < 1 then
time.Value = 0
end
end)
end
local success, value = pcall(function()
return TimeDataStore:GetAsync(tostring(plr.UserId))
end)
if success and value then
time.Value = value
else
time.Value = 900
end
time.Parent = leaderstats
while true do
wait(1)
plr.leaderstats.Time.Value = plr.leaderstats.Time.Value - 1
end
end
local function playerLeaving(plr)
local success, err = pcall(function()
TimeDataStore:SetAsync(tostring(plr.UserId), plr.leaderstats.Time.Value)
end)
if not success then
warn("Failed to save Timer data for player " .. plr.Name .. ": " .. err)
end
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerLeaving)
game:BindToClose(function()
task.wait(10)
end)