Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

1 comment

0

Correction, the localscript fires a remote event whenever the mousebutton1 is clicked.

2 answers

Try:

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

on line 2

1 comment

0

Doesn't change anything.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.