Problems With Chopping / Mining System

What do I want to do?
I wanna make, so when I hold my Axe and click it, and it touches a tree, I want the tree health to go down and a leaderstats value called Chops goes up by a Value, “AxeStrength”. When the tree has no health remaining in the value “TreeHealth” I want the tree to Dissappear and Come back after 30 seconds.

image
image

I’ve tried watching tutorials and viewing through the script.


local TH = game.ReplicatedStorage.TreeHit -- Can use if you want too.
local axe = game.StarterPack.Axe
local AxeStrength = axe.AxeStrength
local head = axe.Head
local tree = script.Parent
local TreeHealth = tree.TreeHealth
local AxeName = axe.AxeName
local Active = tree.ActiveOrNot -- I added this BoolValue, which Imma use later.
local TreeMaxHealth = 25

local plr = game.Players.LocalPlayer
local leaderstats = plr:WaitForChild("leaderstats")

tree.Touched:Connect(function(hit)
	if hit.Parent.Name == axe.Name then -- I've also tried hit.Name
		leaderstats.Chops.Value = leaderstats.Chops.Value + AxeStrength.Value
		print("Tree got Hit by " .. AxeName.Value)
		TreeHealth.Value = TreeHealth.Value - AxeStrength.Value
	end
	if TreeHealth.Value <= 0 then
		Active.Value = false
		wait(30)
		Active.Value = true
	end
end)

while true do
	wait()
	if Active.Value == true then
		tree.Transparency = 1
		tree.CanCollide = false
		tree.CanTouch = false
	elseif  Active.Value == false then
		tree.Transparency = 0
		tree.CanCollide = true
		tree.CanTouch = true
	end
end

Any help is amazing, Only help if you’ve got time!

So what is the issue you are having? This post does not really explain on what the issue is/what the code is not doing.

Also are any of the prints which you have printing out?

1 Like

Not sure exactly what the issue you’re having is, but just a tip: you should get used to attributes, they’re great and much more convenient.

1 Like

The Code doesnt really do anything, it doesnt recognize the if its the Axe that hits the tree. so it doesnt do anything from beyond, ill get some screenshots from the errors

image
Only 1 so far.

Is this in a local script or server side script?

1 Like

It’s a server side script. Should I chang it to a local?

Thanks, Imma begin using them after this is done, still alot more to do in the game!

Ok so the reason why your getting that error is because you try to get the player via game.Players.LocalPlayer

This can only be used in a local script as shown in the documentation.

The reason why you get the error is because you don’t get a player via the localplayer part it can’t find the leaderstat ur looking for.

To fix this issue I think you can just do game.players.PlayerAdded and then get the player from that.

Players.LocalPlayer (roblox.com)

1 Like

Sorry I didn’t notice that, I’m pretty new to scripting!

1 Like

To fix that you can either do what I said or you could like you said do it via a local script but then the issue you would have is you can’t change the leaderstats via a local script so you would then need to fire a remote event which would just make it more complicated so I think if you just do the player added and connect it to a function it should work.

1 Like

Just try that. I think that should then fix all issues. If it still does not work then tell me and I will try to help with any other issues.

1 Like


image
I probably didn’t do the function right.

Yes you did it incorrectly. Just do this:

game.Players.PlayerAdded:Connect(function(plr)
CODE HEREE
end)

The plr is what is given to you from the playeradded event and plr is the player.

2 Likes

Thanks mate, this will help alot!

Sorry for another quick question, but how do i make so the player also needs to click?
And debounce, since i have a really hard time with Debounce

Sorry for the late response. You could put in a click detector inside. I will leave a link to both below. Just look at the different things there and you should find the docs explaining it all. ( you would use the mouseClick. I think it should also work on mobile players even though it says mouse.

ClickDetector (roblox.com)

For the debouce how they work is basically just a if statement to see if something is true. I will leave the Roblox dev documentation below. It is explained well also with some example code. If you don’t know it is often used if something may run more then once (e.g if you want a death part put you only want it to kill the user a little bit over amount of time).

Debounce – When and Why (roblox.com)

1 Like

But I don’t want the player to click on the tree, but to click any where on the screen, and if the axe touches the tree, I want the rest of the script to do its job.

Ah I see. You would then need to use the userinputservice. I am not totaly sure on what you would uses cuz I have not used the userinputservice that much but if you take a look around on the docs you should find what you want (probs would be in the events section).

UserInputService (roblox.com)

You would first detect if something is touching the tree and if there is using an if statment you would check if it is an axe (could do this as well instead by seeing if anything is touching the axe). If it is the tree then check if someone is clicking. I think you could do this the other way round as well.

1 Like