Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to Run a Function When a Part is Touched and MouseButton1 is Pressed?

Asked by 4 years ago

Hello, I recently been trying to make a tree that can be cut down when the player is touching the trunk and their mousebutton1 is down. I made a localscript firing whenever the player clicks the mousebutton1down. I have made a script inside of trunk, the trunk of my tree. Here is the script:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent.Humanoid ~= nil then
        game.ReplicatedStorage.RemoteEvents.Punch.OnServerEvent:Wait()
        script.Parent.Parent.BillboardGui.HealthString.Health.Value = script.Parent.Parent.BillboardGui.HealthString.Health.Value - 1
    end
end)

I am having issues with this script, and would be happy to find a good alternative if possible. The problem exactly is, the value named "Health," isn't changing when I touch it and click my MouseButton1.

0
Correction, the localscript fires a remote event whenever the mousebutton1 is clicked. ImNotKevPlayz 4 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

Try:

if hit.Parent:FindFirstChild("Humanoid") then

on line 2

0
Doesn't change anything. ImNotKevPlayz 4 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Oh wait, fixed it. The reason why it doesn't work was because the trunk was untouchable for some reason.

Answer this question