How can I implement wipeouts and knockouts in this leaderboard script?

Hey there! I want to be precise as much as possible for this. Please read this before commenting.

EXPLANATION
To clarify in detail, I want to make it to where the game has 4 visible currencies that the player can see in leaderboards and GUI’s (which I’m able to code in). I was able to implement the wipeouts and knockouts in this script- but I have a respawn system added in the game so the player can reload. However, it would make the wipeouts go up 10 times! That’s not what I want! I want a normal wipeout system, also having a knockout system in this leaderboard script as well that is able to SAVE.

There’s two other currencies involved in this script, which are exchangeable via gamepasses or by gaining them in the game. These currencies are able to be saved.

SUMMARY
I want to make it to where this script can handle 4 types of currencies all at once. Two which are being collectibles, whereas the other two are wipeouts and knockouts. The script won’t save the wipeouts or knockouts, or will glitch and skyrocket the wipeouts by 10.

WHAT HAVE YOU DONE ALREADY?
I have tried using folders within the ServerScriptService and tried to make them separate, all the while having a different datastore. I also tried putting them together in the same script with different datastores as well. Nothing seemed to work. I use tables to save the datastores.

WHAT DO YOU NEED HELP WITH?
I want someone to teach me how to implement this type of code into this script. Stats that are wipeouts and knockouts, and can be saved. I’m an amateur at scripting big things still, and can only do small things with GUI’s and custom animations- so please explain in simple terms!
BONUS POINTS if you can directly edit my script and tell me what I did wrong, basically telling me what the code should be like.
Thank you for reading. Here’s the code without the wipeouts or knockouts.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CashStore = DataStoreService:GetDataStore("Currency")

local data = {
	["Gold"] = 0,
	["Voids"] = 0
}

Players.PlayerAdded:Connect(function(Player)
	local DataFromStore = nil

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

	for name, value in pairs(data) do
		local new = Instance.new("NumberValue")
		new.Name = name
		new.Value = value
		new.Parent = leaderstats
	end

	local s, e = pcall(function()
		DataFromStore = CashStore:GetAsync("uid-" .. Player.UserId)
	end)

	if s then
		print("Getting Data For: " .. Player.Name)
		if DataFromStore then
			for name, value in pairs(DataFromStore) do
				Player.leaderstats[name].Value = value
			end
			print("Data Loaded For: " .. Player.Name)
		else
			print("Created New Data For: " .. Player.Name)
		end
	else 
		warn("Error Getting Data For: " .. Player.Name)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local DataForStore = {}

	for name, value in pairs(Player.leaderstats:GetChildren()) do
		DataForStore[value.Name] = value.Value
	end

	local success = pcall(CashStore.SetAsync, CashStore, "uid-" .. Player.UserId, DataForStore)

	if success then
		print("Successfully Saved Data For: " .. Player.Name)
	end
	
end)
1 Like
local players = game:GetService("Players")

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

	local knockouts = Instance.new("IntValue")
	knockouts.Name = "Knockouts"
	knockouts.Parent = leaderstats

	local wipeouts = Instance.new("IntValue")
	wipeouts.Name = "Wipeouts"
	wipeouts.Parent = leaderstats
end)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CashStore = DataStoreService:GetDataStore("Currency")

local data = {
	["Gold"] = 0,
	["Voids"] = 0
}

Players.PlayerAdded:Connect(function(Player)
	local DataFromStore = nil

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local knockouts = Instance.new("IntValue")
	knockouts.Name = "Knockouts"
	knockouts.Parent = leaderstats

	local wipeouts = Instance.new("IntValue")
	wipeouts.Name = "Wipeouts"
	wipeouts.Parent = leaderstats
	
	for name, value in pairs(data) do
		local new = Instance.new("NumberValue")
		new.Name = name
		new.Value = value
		new.Parent = leaderstats
	end

	local s, e = pcall(function()
		DataFromStore = CashStore:GetAsync("uid-" .. Player.UserId)
	end)

	if s then
		print("Getting Data For: " .. Player.Name)
		if DataFromStore then
			for name, value in pairs(DataFromStore) do
				Player.leaderstats[name].Value = value
			end
			print("Data Loaded For: " .. Player.Name)
		else
			print("Created New Data For: " .. Player.Name)
		end
	else 
		warn("Error Getting Data For: " .. Player.Name)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local DataForStore = {}

	for name, value in pairs(Player.leaderstats:GetChildren()) do
		DataForStore[value.Name] = value.Value
	end

	local success = pcall(CashStore.SetAsync, CashStore, "uid-" .. Player.UserId, DataForStore)

	if success then
		print("Successfully Saved Data For: " .. Player.Name)
	end
end)
2 Likes

Thanks! This code seemed to do the trick, but I’m now in another loophole.
I was trying to code in that if a person gets killed by another player, or falls through a kill brick, there’d be a value added into it.

Is there a way that I can implement this in?

1 Like