MouseButton1 Animation

Hi guys, could you help me.What I want to do but I can’t figure out how.I want that if the player is standing on the part and he presses the mouse button 1, then the animation.

So you want the Player while standing on a Part, to press a Mouse button?

Can you add more details if possible?

One of the easiest way is with 2 Parts and a ClickDetector

_E = script.Parent -- I used a Folder

_P = _E.Part1 
_C = _E.Part

_T = false -- Our Detector to see if we are Touching the Part or not


_P.Touched:Connect(function(_H)
	if _H.Parent:FindFirstChildOfClass("Humanoid") then -- Checks if the object touching is a Humanoid
		_T = true -- Sets to true
	end
end)

_P.TouchEnded:Connect(function()
		_T = false
end)

_C.ClickDetector.MouseClick:Connect(function()
	if _T == true then -- Checks if _T is set to true
		print("Worked") -- Prints if it worked
	else
		print("Didnt Work") -- prints if it didnt
	end
end)

I believe so, but what exactly do you want to use it for?

I want to use this to collect dirt

1 Like

The use of Mouse is no longer recommended and i suggest you use UserInputService or even ContextActionService instead.

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent) --This event fires when any type of input began/is pressed
	print(input) --> InputObject this is an object that holds all the information about the input
	print(gameProcessedEvent) --> This is a boolean that tells if the game processed the input (ex: if the player was typing this will be false)
	
	if not gameProcessedEvent then --If the player was clicking something else or doing another action then let's ignore this
		if input.UserInputType == Enum.UserInputType.MouseButton1 then --Was the input a mouse click?
			print("Mouse clicked!")
		end
	end
end)

As for your original problem you can use Raycast, like so:

local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")

local player = players.LocalPlayer

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if not player.Character then --if the player is not spawned in then stop
				return
			end

			local params = RaycastParams.new()
			params.FilterDescendantsInstances = {player.Character} --The raycast will ignore the character

			local humanoid = player.Character.Humanoid

			local result = workspace:Raycast(player.Character.HumanoidRootPart.Position, Vector3.new(0, -1, 0) * 15, params)

			if result and player.Character.Humanoid.FloorMaterial ~= Enum.Material.Air then --Checks if there was a result and the player is actually standing on something 
				print("Player clicked while standing on " .. result.Instance.Name)
			end
		end
	end
end)

Thank you very much!You are the best! :kissing_heart:

Please don’t forget to mark the post as the solution if it fixed your issue so the topic can be closed.

How can this be done?I don’t know.

Near the reply button there should be a Checkmark, simply click it and that post will marked as the answer

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.