Need help with making money system

Hello, im making a hitbox system where when the player touch another player’s hitbox the another player who has the hitbox dies, so i want to make the player gets money per kill +30, so i need help making a money system in leaderboard, please let me know if u can help.

Hitbox System

local Players = game:GetService(“Players”)

– Function to disable collision for character parts
local function disableCollision(character)
for _, part in pairs(character:GetChildren()) do
if part:IsA(“BasePart”) then
part.CanCollide = false
end
end
end

– Function to get the size of the player’s torso
local function getTorsoSize(player)
local character = player.Character
if character then
local torso = character:FindFirstChild(“Torso”) or character:FindFirstChild(“UpperTorso”)
if torso then
return torso.Size.Magnitude
end
end
return 0 – Return 0 if no torso is found
end

– Function to create the kill part
local function createKillPart(player)
local character = player.Character
if character then
local torso = character:FindFirstChild(“Torso”) or character:FindFirstChild(“UpperTorso”)
if torso then
– Disable collision for character parts
disableCollision(character)

		-- Create a new part
		local killPart = Instance.new("Part")
		killPart.Size = Vector3.new(1, 1, 1) -- Set the size of the part
		killPart.Transparency = 1 -- Make the part invisible (optional)
		killPart.CanCollide = false -- Disable collision (optional)
		killPart.Anchored = false -- Allow the part to move with the player
		killPart.Name = "KillPart"

		-- Parent the part to the character
		killPart.Parent = character
		-- Position the part inside the torso
		killPart.Position = torso.Position

		-- Weld the part to the torso to move with it
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = torso
		weld.Part1 = killPart
		weld.Parent = torso

		-- Connect a Touched event to the killPart
		local debounce = false
		killPart.Touched:Connect(function(hit)
			if debounce then return end -- Prevent multiple triggers at once
			debounce = true
			local otherPlayer = Players:GetPlayerFromCharacter(hit.Parent)
			if otherPlayer and otherPlayer ~= player then
				local playerTorsoSize = getTorsoSize(player)
				local otherPlayerTorsoSize = getTorsoSize(otherPlayer)
				-- Check if the player's torso size is greater than the other player's torso size
				if playerTorsoSize > otherPlayerTorsoSize then
					local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
					if humanoid then
						humanoid.Health = 0
					end
				end
			end
			debounce = false
		end)
	end
end

end

– Connect the PlayerAdded event to spawn the kill part and disable collision
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
createKillPart(player)
end)
end)

2 Likes

So if you want a simple leaderboard…

Create a script inside ServerScriptService and add this leaderboard system with datastore

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PointsStats") -- Name it how you want.

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	local Currency = Instance.new("StringValue")
	Currency.Name = "Points" -- You can name this "Money", "Points", Or how you want!
	Currency.Value = DataStore:GetAsync(Player.UserId) or "0"
	Currency.Parent = Leaderstats
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, Player.leaderstats.Points.Value)
end)

So now if you want to add money to the player who killed the other one
Add this lines on your code:

local HowMuchMoneyGive = 35
Player:WaitForChild("leaderstats"):FindFirstChild("Points").Value = Player:WaitForChild("leaderstats"):FindFirstChild("Points").Value + HowMuchMoneyGive
2 Likes

Where should i add the lines in my code? Please can you provide the full script? with the lines

Hope I’m not too late! But here’s the script you asked for (I left some notes for you)

(Also I didnt include the code for getting money, though its simple to do)

This is the code you’re asking for:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore") --Currency name here


Players.PlayerAdded:Connect(function(player)
	local PlayerData = DataStore:GetAsync(player.UserId)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Money = Instance.new("IntValue") --Change this to a number value for decimals
	Money.Name = "Money" --Change to your currency name
	Money.Parent = leaderstats
	Money.Value = PlayerData or 0 --Change this to the cash you want a new player to start with
	
	player.CharacterAdded:Connect(function(character)
		createKillPart(player)
	end)
end)

local function SaveData(player)	
	local succ, err = pcall(function()
		DataStore:SetAsync(player.UserId, player.leaderstats.Money.Value) --Change "Money" to your currency name
	end)
	
	if err then
		warn("Error saving data:", err) --Prints the error so you can fix it
	end
end

Players.PlayerRemoving:Connect(function(player) --Save data when player leaves
	SaveData(player)
end)

game:BindToClose(function() --If you ever shutdown your game it'll save data
	for _, player in ipairs(Players:GetPlayers()) do
		SaveData(player)
	end
end)

This is the entire code:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore") --Currency name here

local function disableCollision(character)
	for _, part in pairs(character:GetChildren()) do
		if part:IsA("BasePart") then
			part.CanCollide = false
		end
	end
end

local function getTorsoSize(player)
	local character = player.Character
	if character then
		local torso = character:FindFirstChild("Torso") or character:FindFirstChild("UpperTorso")
		if torso then
			return torso.Size.Magnitude
		end
	end
	return
end


local function createKillPart(player)
	local character = player.Character
	if character then
		local torso = character:FindFirstChild("Torso") or character:FindFirstChild("UpperTorso")
		if torso then
			disableCollision(character)

			local killPart = Instance.new("Part")
			killPart.Size = Vector3.new(1, 1, 1)
			killPart.Transparency = 1
			killPart.CanCollide = false
			killPart.Anchored = false
			killPart.Name = "KillPart"

			killPart.Parent = character
			killPart.Position = torso.Position

			local weld = Instance.new("WeldConstraint")
			weld.Part0 = torso
			weld.Part1 = killPart
			weld.Parent = torso

			local debounce = false
			killPart.Touched:Connect(function(hit)
				if debounce then return end
				debounce = true
				local otherPlayer = Players:GetPlayerFromCharacter(hit.Parent)
				if otherPlayer and otherPlayer ~= player then
					local playerTorsoSize = getTorsoSize(player)
					local otherPlayerTorsoSize = getTorsoSize(otherPlayer)

					if playerTorsoSize > otherPlayerTorsoSize then
						local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
						if humanoid then
							humanoid.Health = 0
						end
					end
				end
				debounce = false
			end)
		end
	end
end

Players.PlayerAdded:Connect(function(player)
	local PlayerData = DataStore:GetAsync(player.UserId)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Money = Instance.new("IntValue") --Change this to a number value for decimals
	Money.Name = "Money" --Change to your currency name
	Money.Parent = leaderstats
	Money.Value = PlayerData or 0 --Change this to the cash you want a new player to start with
	
	player.CharacterAdded:Connect(function(character)
		createKillPart(player)
	end)
end)

local function SaveData(player)	
	local succ, err = pcall(function()
		DataStore:SetAsync(player.UserId, player.leaderstats.Money.Value) --Change "Money" to your currency name
	end)
	
	if err then
		warn("Error saving data:", err) --Prints the error so you can fix it
	end
end

Players.PlayerRemoving:Connect(function(player) --Save data when player leaves
	SaveData(player)
end)

game:BindToClose(function() --If you ever shutdown your game it'll save data
	for _, player in ipairs(Players:GetPlayers()) do
		SaveData(player)
	end
end)
1 Like