Leaderstats not giving starting amount

Greetings,

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)

I need it so that the number doesn’t go negative.

I see; I have edited my previous response and have now only changed it under the if success and value then if statement. Tell me if it works now.

1 Like

It’s still not changing anything, I might have to change the code for the negative numbers.

Have you tried @Coco_Verified’s script to see if the player’s value gets set to 900, when the player first joins the game?

Yes, I did try that also. nothing seemed to change.

Does the pcall ever error? Try printing if it’s a success

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)

Yes, I placed the script in ServerScriptService inside a Folder. I’ll try to test on a new account that doesn’t have any data created.

image

1 Like

Good news, it’s finally working.

1 Like

Sorry to hear it didn’t work, I’m not able to work it out so I cant give any more help. Sorry, wishing you the best resolving the issue though!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.