Needing help with leaderstats!

Hello, I’m making a fighting game where there are two leaderboard stats, Kills and All time Kills. I want the alltime kills to save with a datastore but for some reason it isnt working. I don’t get ANY output errors so I don’t know what’s wrong. Here is my code:

local players = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local playerAllTimeKillsDataStore = dataStores:GetDataStore("All Time Kills")

players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name =  "leaderstats"

	local playerKills = Instance.new('NumberValue')
	playerKills.Name = "Kills"
	playerKills.Parent = leaderstats
	local playerAllTimeKills = Instance.new('NumberValue')
	playerAllTimeKills.Name = "All Time Kills"
	playerAllTimeKills.Parent = leaderstats

	task.wait(1)

	local playerAllTimeKillsData = playerAllTimeKillsDataStore:GetAsync(plr.UserId)


	if playerAllTimeKillsData then
		print("Player has data.")
		playerAllTimeKills.Value = playerAllTimeKillsData
		if playerAllTimeKills.Value >= 1 then
			print("You already have some kills.")
		end

	else
		print("Player has no data.")
		playerAllTimeKillsDataStore:SetAsync(plr.UserId, 0)
		print("Player now has data.")
	end
end)

players.PlayerRemoving:Connect(function(plr)
	local leaderboardThing = plr:WaitForChild("leaderstats")
	local playerAllTimeKills = leaderboardThing:WaitForChild("All Time Kills")
	playerAllTimeKillsDataStore:SetAsync(plr.UserId, playerAllTimeKills.Value)
end)

– I have studio access to api services enabled
– I only want AllTimeKills to save as a datastore, not kills.

If anyone knows how to make my code actually save AllTimeKills Data, please reply down below!

3 Likes

Sometimes it doesn’t save datastore when testing the game on studio.

4 Likes

I don’t know a lot about datastore, actually it’s my biggest nightmare. But i think that it’s not working because you created the folders inside the PlayerAdded function. Maybe creating the folders in a separate script and accessing it from that one will work. But i could be wrong, i barely know how datastoring works.

hope your game does well

4 Likes

Join the game from roblox site then test it again

3 Likes

No, i used this same datastore type script for my other game and it worked even when in studio.

4 Likes

That could work… I’ll try it after I test some of my own ideas…

3 Likes
local players = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local playerAllTimeKillsDataStore = dataStores:GetDataStore("All Time Kills")
local leaderstats, allTimeKills = nil, nil

local function savePlayerData(plr)
	leaderstats = plr:FindFirstChild("leaderstats")
	if leaderstats then allTimeKills = leaderstats:FindFirstChild("All Time Kills")
		if allTimeKills then

			local sus, err = pcall(function()
				playerAllTimeKillsDataStore:SetAsync(plr.UserId, allTimeKills.Value)
			end) if not sus then warn("Save Data Error: " .. err) end		
		end
	end
end

players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local playerKills = Instance.new("NumberValue")
	playerKills.Name = "Kills"
	playerKills.Parent = leaderstats

	local playerAllTimeKills = Instance.new("NumberValue")
	playerAllTimeKills.Name = "All Time Kills"
	playerAllTimeKills.Parent = leaderstats

	local data = 0
	local sus, err = pcall(function()
		data = playerAllTimeKillsDataStore:GetAsync(plr.UserId) or 0
	end) if not sus then warn("Load Data Error: " .. err) end	
	playerAllTimeKills.Value = data
end)

players.PlayerRemoving:Connect(savePlayerData)

game:BindToClose(function()
	for _, plr in pairs(players:GetPlayers()) do
		savePlayerData(plr)
	end
end)

SavePlayerData() is also link with BindToClose, for shut downs. So it’s best for this version to use FindFirstChild and quick check. Loads data on join, saves data on exit along with a shut down trap.
Added load/save data error warnings … helps if data error to not disable the script and debugging.

1 Like

but sometimes it doesn’t work. You don’t need to change the code.

2 Likes

Problem 1: It breaks my punch code (The animation and the damage dealing), if I use my original non-functional script the punch script works, maybe there’s an interference of some sort?

Problem 2: Due to problem 1, I am unable to gain kills so I can test whether this code works or not.

1 Like

Looks like I enter this wrong , for the leaderboard… you’ll have to go over it.
Sorry I try not to use spaces it’s habit now,

1 Like

nevermind i fixed it after some tweaking

1 Like

the data saving still doesn’t work… im pretty sure i fixed all the errors because nothing bad in output but maybe i missed something…

1 Like

Do you have API services set to on in the game settings? Guess we should have started there.

1 Like

set up? I don’t know what you mean…

1 Like

i put print statements in the SavePlayerData function, they output

“We found the leaderstats”
“We found all time kills”
this is weird… is there something wrong with Setasync?

1 Like

Did you also add spaces to this one?

Example: DATA000 test program

I’ve repost the above with the spaces needed.
API services set to on from the menu: Home/Game Settings/Security

1 Like

And sometimes you get a perfectly formatted template from someone that had problems at 1st too.

1 Like

yes i did, so idk whats wrong

maybe i won’t include all time kills for now and release them later along with a possible physical leaderboard update?

because beside this datastore system my game is generally complete.

1 Like

i set up api a long time ago, idk whats wrong

1 Like

Sorry you’re having problems with it … they sure were a headache for me for also.
Maybe it would be best to wait on that. I left you a link to an editable example.
Getting a solid datasave working for me was a priority, never did get much help.
Good luck with your game.

2 Likes