Need help with a leaderstats saving/calculation

I am trying to write a script that records and saves kills and deaths through games and it has a points system for a leader board in a hub game where you join the smaller games from, you get a point per kill and -1 point per death and I’ve run into trouble with this script. Saving its data to transfer over to the hub game and into other smaller games that are connected to it, and getting a point a kill/losing a point on death isn’t working, I believe this is a logic error because no errors are outputted. Not willing to pay, that was an error I, copy pasted from another one of my posts… Sorry aha I think I’m missing a module script.

There is no error message

Here is a copy of the code and how it tries the calculation. Alongside the leader stats script itself.

local PointsValue = Instance.new("NumberValue", Player)
	PointsValue.Name = "FP"
	PointsValue.Parent = Player.leaderstats
	local FetchedPoints
	
	local succ, err = pcall(function()
		FetchedPoints = FWMPoints:GetAsync(tostring(Player.UserId))
	end)

	if succ then
		PointsValue.Value = FetchedPoints
	end
end

(followed by this wayy at the bottom)

if UpdatePointsLeaderboard and UpdatePointsLeaderboard:IsA("RemoteEvent") then
	while true do
		for _, Player in pairs(Players:GetPlayers()) do
			if Player.UserId <= 0 then
				continue
			end

			while (not PlayerDatas[Player]) and Player do
				task.wait()
			end

			local Find = DataHandler.New(PlayerDatas[Player], Player.UserId)

			if Find and Find:getStats() then
				local playerStats = Find:getStats()

				local success, failed = pcall(function()
					return Points:UpdateAsync(Player.UserId, function()
						return playerStats.KOs - playerStats.WOs
					end)
				end)

				print(success and "Successfully saved!" or failed)
			end
		end

		local sortedTop: Instance | DataStorePages = Points:GetSortedAsync(false, 10)

		if sortedTop and sortedTop:IsA("DataStorePages") then
			local currentTopPlayers = sortedTop:GetCurrentPage()

			UpdatePointsLeaderboard:FireAllClients(currentTopPlayers)
		end

		task.wait(60)
	end
end

(and this is the script for the kills and deaths leader board)

game.Players.PlayerAdded:Connect(function(plr)

	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = plr

	local kills = Instance.new("IntValue")
	kills.Name = "KOs"
	kills.Parent = stats

	local deaths = Instance.new("IntValue")
	deaths.Name = "WOs"
	deaths.Parent = stats

	plr.CharacterAdded:connect(function(char)
		local humanoid
		repeat
			humanoid = char:FindFirstChild("Humanoid")
			wait()
		until humanoid
		humanoid.Died:connect(function() 
			deaths.Value = deaths.Value + 1 
			local tag = humanoid:FindFirstChild("creator") 
			if tag then  
				local killer = tag.Value  
				if killer then   
					killer.leaderstats.KOs.Value = killer.leaderstats.KOs.Value + 1
				end   
			end 
		end)     
	end)
end)
2 Likes

Datastores always return an error, unless they don’t work, which happens when API services are disabled.

You can either play test the game exclusively through the Roblox client, or enable studio access to API services:

First, publish the game, then go to “Game Settings” in the studio home tab, go to security, and click the slider near "Enable Studio Access to API services

Hope I helped!

This does help but I’ve had this on since the games creation!

I can’t really help right now because I am on my phone, but can you please try the “print spam” debugging method?

Just put a print statement after every single “chunk” of code, like print("created leaderstats") after you create leaderstats and the same for other chunks

Then run the game and observe the output for any irregularities in print statements, like a print statement tied to a guaranteed event not outputting, or printing more times then asked for.

Could you also provide the full script? I’ll look into it when I can. Thanks!

alright i ran this with all the print errors and i figured a line wasnt being read, i rewrote it and it works as a charm now cheers

1 Like

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