How do I add a money giver code to my script?

I need a money give code between the “-- log (money) give code” and “–”

(this code is from a tree that when your axe hits it, it gives you logs)

the script:

local Log = script.Parent
local hits = 5
local totalhits = 0
local SpareTree = game.ReplicatedStorage.Tree

local text = script.Parent.BillboardGui.TextLabel

local debounce = false

local maxnum = 5

Log.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		if hit.Parent.Name == "Axe" then
			hits = hits + -1
			
			text.Text = "Health: "..hits.."/"..maxnum
			
			-- log (money) give code
			
			
			
			--
			
			
			if hits == totalhits then
				if hits == totalhits then
					text.Text = "Dead"
					hits = 0 
					Log.Anchored = false
					wait()
					Log.Rotation = Vector3.new(45,0,0)
					wait(3)
					Log:Destroy()
					local NewTree = SpareTree:Clone()
					NewTree.Parent = workspace
					text.Text = "Health: 5/5"
				end
			end
		end
		wait(1)
		debounce = false
	end
end)

my leaderboard (to help you help me)

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"
	
	local logs = Instance.new("IntValue")
	logs.Value = 0
	logs.Parent = leaderstats
	logs.Name = "logs"
	
	local planks = Instance.new("IntValue")
	planks.Value = 0
	planks.Parent = leaderstats
	planks.Name = "planks"
	
end)

here is a video if it helps any.

(this is what it looks like kinda, except it gives you logs)

1 Like

Do you have any type of money system?

Have a look at this, In-Experience Leaderboards | Roblox Creator Documentation. Do you already have a leaderstats system up?

Wdym ‘logs’ and ‘leaderstats’, like do you mean you have a log leaderstat?

local Log = script.Parent
local hits = 5
local totalhits = 0
local SpareTree = game.ReplicatedStorage.Tree

local text = script.Parent.BillboardGui.TextLabel

local debounce = false

local maxnum = 5

Log.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		if hit.Parent.Name == "Axe" then
			hits = hits + -1
			
			text.Text = "Health: "..hits.."/"..maxnum
			
			-- log (money) give code
			
			
			
			--
			
			
			if hits == totalhits then
				if hits == totalhits then
					text.Text = "Dead"
					hits = 0 
					Log.Anchored = false
					wait()
					Log.Rotation = Vector3.new(45,0,0)
					wait(3)
					Log:Destroy()
					local NewTree = SpareTree:Clone()
					NewTree.Parent = workspace
					text.Text = "Health: 5/5"
				end
			end
		end
		wait(1)
		debounce = false
	end
end)

Is this script is on the server?

it is in workspace yes, that script is in the tree

Ok, I assume that you have some sort of ownership value of the axe. If so you can locate the the player’s leaderstats folder and change their log(money) value.

but that’s what im struggling with is how do I locate where the leaderstats is if the axe hits the part?

Do you have any sort of object/value that stores the owner of the axe?

the axe is a tool that the player uses to chop the tree. But I don’t know really what else to say about the “object/value that stores the owner of the axe”.
(not trying to be rude or anything)

When the tree dies, simply add a line of code like this:

game.Players:GetPlayerFromCharacter[hit.Parent.Parent].leaderstats.logs.Value += 1

Ok I see. Where is the axe(tool) stored. Where is it?

in the backpack of the player.

You could get the Player instance from the parent of tool the as @Kaid3n22 said.

So the script should be something like this:

local Log = script.Parent
local hits = 5
local totalhits = 0
local SpareTree = game.ReplicatedStorage.Tree

local text = script.Parent.BillboardGui.TextLabel

local debounce = false

local maxnum = 5

Log.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		if hit.Parent.Name == "Axe" then
			hits = hits + -1

			text.Text = "Health: "..hits.."/"..maxnum
			
			--// Money give.
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent.Parent) --// Grabs the player instanace from the character
			local Leaderstats = Player:FindFirstChild("leaderstats") --// Finds the leaderstat folder in the player instance
			
			Leaderstats.logs.Value += 1 --// Change the (1) to the amount you want to add to their logs(money)
			--// Money give.


			if hits == totalhits then
				if hits == totalhits then
					text.Text = "Dead"
					hits = 0 
					Log.Anchored = false
					wait()
					Log.Rotation = Vector3.new(45,0,0)
					wait(3)
					Log:Destroy()
					local NewTree = SpareTree:Clone()
					NewTree.Parent = workspace
					text.Text = "Health: 5/5"
				end
			end
		end
		wait(1)
		debounce = false
	end
end)

Please mark this as solution if this helps with your problem :smiley: ! If it doesn’t please inform me if you receive any errors! :slightly_smiling_face:

im a DumbA$%… i forgot to duplicate and put a copy of the model into ReplicatedStorage.
because of this code:

local SpareTree = game.ReplicatedStorage.Tree

so the code i originally made before making this post would’ve worked if I paid more attention to that mistake :man_facepalming: