.Touched is not detecting the collision. I am trying to detect if a bullet/ball would collide with a model, which both of them are moving. Any solution? I don't really know how to do raycasting or Region3, if there are any examples, please let me know. Thanks! P.S. The bullet/ball is a model, so I am using SetPrimaryPartCFrame or GetPrimaryPartCFrame.
01 | for i, v in pairs (script.Parent:GetChildren()) do |
02 | if v:IsA( "Part" ) then |
03 | v.Touched:Connect( function (hit) |
04 | if hit.Name = = "Waterball" then |
05 | script.Parent.Damage.Value = script.Parent.Damage.Value - 100 |
06 | script.Parent.Head.BillboardGui.TextLabel.Text = "Damage Remaining: " .. tostring (script.Parent.Damage.Value) |
07 | end |
08 | end ) |
09 | end |
10 | end |
.Touched
is not a very effective way to detect collisions; as stated in a devforum post: you can use BasePart:GetTouchingParts or Region3 for most effective detection.
Region3
is a specific region of space, it's parameters are as follows:
1 | local Reg 3 = Region 3. new(Vector 3. new( 0 , 0 , 0 ), Vector 3. new( 0 , 0 , 0 )) |
Region3
looks at a specific area of space in workspace
.
:GetTouchingParts()
shows what parts are touching that exact area/object. It returns a table. Like so:
01 | local obj = workspace.Part |
02 | local obj 2 = workspace.Part 2 |
03 | while true do |
04 | wait() |
05 | local a = obj 2 :GetTouchingParts |
06 | if table.find(a, obj) then |
07 | print ( "TOUCHING!" ) |
08 | break |
09 | end |
10 | end |
NB - I would not recommend using Region3
for this. You should just use obj:GetTouchingParts().
Hey WoofWoofWatermelonYT,
As far as I can see from your code the reason why .Touched
isn't working is that you're for loop will only run once. Maybe try adding a while true do
to your code like so:
01 | while true do |
02 | for i, v in pairs (script.Parent:GetChildren()) do |
03 | if v:IsA( "Part" ) then |
04 | v.Touched:Connect( function (hit) |
05 | if hit.Name = = "Waterball" then |
06 | script.Parent.Damage.Value = script.Parent.Damage.Value - 100 |
07 | script.Parent.Head.BillboardGui.TextLabel.Text = "Damage Remaining: " .. tostring (script.Parent.Damage.Value) |
08 | end |
09 | end ) |
10 | end |
11 | end |
12 | wait() |
13 | end |
If this helped approve the answer (Optional)
Any questions? Feel free to contact me.
Cheers,
Orcazate