Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you fix this collision for a model? (Model/Bullet or Ball to Model/Boss character)

Asked by 3 years ago
Edited 3 years ago

.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
0
what's the error, what's not working? krowten024nabrU 463 — 3y
0
.Touched is not detecting the collision of the ball/bullet. WoofWoofWatermelonYT 16 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

.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().

0
It does not work. WoofWoofWatermelonYT 16 — 3y
0
It does not detect the block hitting the model. WoofWoofWatermelonYT 16 — 3y
0
I'm not giving you code, no one is you must make it yourself. WideSteal321 773 — 3y
Ad
Log in to vote
0
Answered by
orcazate 170
3 years ago

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

0
Hello, thanks for responding. However, it does not work. WoofWoofWatermelonYT 16 — 3y

Answer this question