.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.
for i, v in pairs(script.Parent:GetChildren()) do if v:IsA("Part") then v.Touched:Connect(function(hit) if hit.Name == "Waterball" then script.Parent.Damage.Value = script.Parent.Damage.Value - 100 script.Parent.Head.BillboardGui.TextLabel.Text = "Damage Remaining: " .. tostring(script.Parent.Damage.Value) end end) end 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:
local Reg3 = Region3.new(Vector3.new(0,0,0), Vector3.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:
local obj = workspace.Part local obj2 = workspace.Part2 while true do wait() local a = obj2:GetTouchingParts if table.find(a, obj) then print("TOUCHING!") break end 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:
while true do for i, v in pairs(script.Parent:GetChildren()) do if v:IsA("Part") then v.Touched:Connect(function(hit) if hit.Name == "Waterball" then script.Parent.Damage.Value = script.Parent.Damage.Value - 100 script.Parent.Head.BillboardGui.TextLabel.Text = "Damage Remaining: " .. tostring(script.Parent.Damage.Value) end end) end end wait() end
If this helped approve the answer (Optional)
Any questions? Feel free to contact me.
Cheers,
Orcazate