I am making a pickaxe script for my game and I can’t seem to make it so that it only works if you click on stone (terrain editor). Any help is appreciated
The script:
if (Input.UserInputType == Enum.UserInputType.MouseButton1) and (not Processed) then
local RayCast = game:GetService("Workspace").CurrentCamera:ScreenPointToRay(Input.Position.X, Input.Position.Y)
local rayResult = game:GetService("Workspace"):Raycast(RayCast.Origin, RayCast.Direction * 100)
if (rayResult.Instance.Name) then
print(rayResult.Instance.Name)
if rayResult.Instance.Name ~= "Stone" then return end
end
end
end)``` it's called when the tool is activated
Thanks
It looks like you’re on the right track with your script! Here are a few suggestions to help you ensure that the pickaxe only works when clicking on stone:
Check if rayResult.Instance exists before checking its name. If the raycast does not hit anything, rayResult.Instance will be nil and checking its name will result in an error.
Use the :FindFirstChild() method to check if the hit part is a descendant of the “Stone” object. This will also allow the script to work if the player clicks on a child of the “Stone” object, such as a texture.
Here’s an updated version of your script:
if Input.UserInputType == Enum.UserInputType.MouseButton1 and not Processed then
local ray = game.Workspace.CurrentCamera:ScreenPointToRay(Input.Position.X, Input.Position.Y)
local hit = game.Workspace:Raycast(ray.Origin, ray.Direction * 100)
if hit and hit.Instance:FindFirstChild("Stone") then
-- Do your pickaxe action here
-- ...
end
end
I hope this helps! Let me know if you have any further questions.