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

How to Make Detection Script Work With More Efficiency?

Asked by 4 years ago
Edited 4 years ago

Basically I want to find a way to make my script more efficient when detecting if an object hits the part above the basketball rim.

There is a part that detects if the basketball touches the top of the rim.

OnTop Code: (Sets the OnTop value to true if it touches the top of the rim.)

local brick = script.Parent
local OnTop = script.Parent.Parent.OnTop

brick.Touched:Connect(function(hit)
    if hit.Name == "Ball" then
        OnTop.Value = true
        wait(1)
        OnTop.Value = false
    end
end)

Goal Code: (Basically if it goes through the basket and checks if the OnTop value is set to true)

while not _G.AeroServer do wait() end
local HoopModule = _G.AeroServer.Modules.HoopModule

local hoop = script.Parent.Parent
local ontop = hoop.OnTop
local brick = script.Parent

brick.Touched:Connect(function(hit)

    if hit.Name == "Ball" and ontop.Value == true then
        HoopModule:Hit(hoop, script) --Basically makes a part flash green
    end

end)

Visual Representation

This picture below shows where everything is located.

Image Reference

Right now, the scripts work okay, but there are a couple instances where the flashing green part won't light up.

Thanks, LukeGabrieI

0
Just combine both scripts into one script, you don't need two touched events! firestarroblox123 440 — 4y
0
I mean you don't need two touched events on the same part. firestarroblox123 440 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Touched events are kinda terrible to work with, and not really a sound way to detect if the shot was actually made. If you want to do the detection this way--with two invisible parts--the 1-second wait(1) mechanism should be removed from the first script. The script on the Goal part should reset the OnTop flag, not a timer. If you have to space the parts such that the ball can still be touching the OnTopPart as it hits the Goal part, then it would make sense to put a short cooldown on resetting the OnTop flag but in the Goal part's script, not the OnTopPart's script.

That said, I wouldn't solve the problem this way, because it doesn't actually validate whether or not the ball passed through the hoop, which is the only measure of whether or not it's a scoring shot. You're detecting a set of events that correlates with goal, but which leaves room for the events to occur even without the ball passing through the hoop. Maybe this can't happen if you're just making a 3-pointer contents, but if it's a full game of basketball it's not good enough.

What you really ought to do is track the path of the ball and detect if it actually passes through the circle in space that represents the wire rim. i.e. did the ball go from being above the plane of the rim to below it, with a segment of the ball's path (between successive frames) intersecting the disc inside the rim circle, rather than all of the rest of the plane outside the circle. That's the rigorous geometric way of detecting the goal without false positives or negatives being possible.

This will be way more efficient than Touched events, because the broad-phase check is at most 2 inequality compares. Calculation is only done on path segments that intersect the plane from top to bottom, which is obviously very infrequent (once per shot or high bounce).

0
This is not hard math, btw, to find out if the path went through the hoop. It's just a line-plane intersection, and the plane is just a Y value. then you just need to check if the distance between this intersection and the center of the hoop is less than the hoop radius. EmilyBendsSpace 1025 — 4y
0
how do I check the path of the ball, quadratics? LukeGabrieI 73 — 4y
Ad

Answer this question