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.
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!
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
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.
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.
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).
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).
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.