Hello and I have problems with .Touched event, It doesn’t work if the other part isn’t moving, It looks like this.
That is because .Touched
only gets fired when there is physical movement. In Roblox Studio, it reads quote:
“Fires when a part touches another part as a result of physical movement.”
That means when there is no movement, it does not get fired. If you want something to fire while touching, you can run a while
loop in a .Touched
, and then disable the while
loop in a .TouchEnded
event.
Edit: I made a quick over-complicated mockup to show in summary what I mean. Note that this is a ServerScript as the child of the part that will run the event when touched. It currently checks just for touching BaseParts.
local part = script.Parent
local isTouching = false
local debounce = false
part.Touched:Connect(function(hit)
debounce = true
isTouching = true
if hit:IsA("BasePart") then
debounce = true
print("Touching part.")
while isTouching == true do
task.wait(0.1)
debounce = false
--//Execute your function here.
end
end
print("Nevermind.")
end)
part.TouchEnded:Connect(function()
isTouching = false
end)
Touched is very unreliable in this case (like the person above said), you should probably use workspace:GetPartsInPart()
instead
local part — part
while true do — loop
task.wait(0.1)
for i, v in workspace:GetPartsInPart(part) do — loop through parts touching the part
if v.Name == “partname” then — check if the part is touching another part with specified name
— touching
end
end
end
You can try using:
part:GetTouchingParts() -- {}
It works, but it doesn’t work with CanCollide off parts, how can I make it work in non collidable parts?
Use GetPartsInPart instead, it works on both collidible and non-collidible parts
I don’t know how to use OverlapParams.
useful documentation about spatial queeries:
be warned though- I’m fairly certain that if you set off CanQueery
to false, it will disable spatial queeries for that part.
You don’t need one in this case, it’s just optional to use it to filter out parts. If you want, use the documentation that @SkrubDaNub posted
It worked! Thank you so much!!!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.