I’m able to touch everything else just fine, including the terrain which surrounds the model, but whenever I try and touch the model with the handle, nothing is printed out. Unsure why exactly it’d only affect the ores, but, it is. Below you’ll find the little bit of code that I wrote. Any help would be appreciated!
local Tool = script.Parent
local Handle = Tool.Handle
Handle.Touched:Connect(function(hit)
local h = hit.Parent:FindFirstChildOfClass("Model")
if h then
print(hit.Name)
if hit.Name == "Rock" and hit:IsA("Model") then
print("Rock Test")
end
end
end)
I’m confused, if the handle may be touching parts inside the rock model, shouldn’t you be checking whether hit.Parent is a model? Why are you checking if the hit part itself is a model?
Nuh uh, you still have another check seeing if hit is a Model, even though touched events can only be fired by a BasePart. If you want to detect whenever the handle touches this model use this code.
local Tool = script.Parent
local Handle = Tool.Handle
Handle.Touched:Connect(function(hit)
if hit.Parent:IsA("Model") and hit.Parent.Name == "Rock" then -- The IsA function returns a boolean depending on if the instance's class matches the string provided
print("rock touched")
print("Part Name is "..hit.Name)
-- do whatever you want when handle touches rock here
end
end)
This works as intended, however, now I’m facing a new problem. How would I go about adding an active/inactive buffer, so that the ore isn’t bombarded with information. This is what I’ve got currently, which isn’t much.
local Tool = script.Parent
local Handle = Tool.Handle
local Active = nil
if Tool.Activated and not Active then
Active = true
elseif Tool.Deactivated and Active then
Active = false
wait(3)
end
Handle.Touched:Connect(function(hit)
if hit.Parent:IsA("Model") and hit.Parent.Name == "Rock" and Active then
print("Rock Test")
end
end)
I assume you are talking about a cooldown for hitting the ore? If so, you can try doing something like this:
local Tool = script.Parent
local Handle = Tool.Handle
local cooldown = 3 -- (in seconds)
local on_cooldown = false -- this is where you'll store the boolean
Handle.Touched:Connect(function(hit)
if hit.Parent:IsA("Model") and hit.Parent.Name == "Rock" and not on_cooldown then -- isnt true if the rock has been hit recently
print("Rock Test")
-- decrease the ore's health here
on_cooldown = true -- any other events with this code wont work now
task.wait(cooldown) -- wait for a bit
on_cooldown = false -- now the other events can print again
end
end)