I have a brick trigger system for certain events in my game. I want the trigger to collide with players only so I made them collide only with players using collision groups. However touched event is fired no matter what collision group the basepart has. Is this the way it should be?
I have already solved the issue, but I still have the question left.
Here’s an example:
https://i.gyazo.com/e02317fe8308cd0d1c61628f80242fd0.mp4
A ducktape solution would be to ignore the part if it’s collision group is the one you want to avoid.
Example:
--Touched event
if hit.CollisionGroup == "Blue" then return end
The reason I had an issue is because I used
BasePart.Touched:Wait()
If the check fails, how would I yield again?
Instead of that I would make a connect:
BasePart.Touched:Connect(function()
--if statements
--code
end)
Could do something like
while true do
if physicsServ:CollisionGroupContainsPart("Blue", BasePart.Touched:Wait()) then
break
end
end
But I think that creating a connection and then disconnecting would be better.
Extension to what @ReturnBreakEnd said, if you want only to check once, you can disconnect the event after it succeeds
This is how I do it personally:
local event
event = BasePart.Touched:Connect(function(hit)
if hit.CollisionGroup == "Blue" then
event:Disconnect()
end
end)
It’s way better than using :Wait(), but how would you yield the current thread with this?
Note that CollisionGroup is not a value of parts. CollisionGroupId is, but that’s the numerical id of the collision group!
local event
local bind = Instance.new('BindableEvent')
event = BasePart.Touched:Connect(function(hit)
if hit.CollisionGroup == "Blue" then
event:Disconnect()
bind:Fire()
bind:Destroy()
end
end)
bind.Event:Wait()
I think there’s no need to create a BindableEvent for this
local event
event = BasePart.Touched:Connect(function(hit)
if hit.CollisionGroup == "Blue" then
event:Disconnect()
end
end)
repeat wait() until event.Connected == false
repeat wait() until
is considered bad practise, which is why I used a bindable.