How Do I Detect the Basketball Coming from the Top of the Hoop?

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

1 Like

Just a heads up when posting topics with code, be sure to wrap your code in three backticks (`) so it is formatted correctly.

In regards to your problem here you say that there are instances where the flashing green part doesn’t light up, do you have examples of those cases? Could we see a sample video perhaps? Your image reference and visual representation hyperlinks both go to the same thing

Sorry I just fixed it, I was using a mobile device to ask this question.

Is it possible to get pictures of the hitboxes for each part? The information provided is useful, but more visuals would help since the issue seems based around the .Touched event not triggering consistently.

Top Part and Goal Part

I’d recommend adding a type of ā€˜debounce’, or delay for when OnTop is set to true. I’m unsure if this is what’s causing the problem, but as it is now, it’s rapidly setting OnTop to true, waiting a second, then rapidly setting OnTop to false. Since you’re changing a value from false to true anyways, you don’t actually need to add a variable for a debounce. You can just add to the conditional statement:

if hit.Name == "Ball" and not OnTop.Value then 

In any case, it’s unlikely but possible that the .Touched isn’t registering properly due to the thickness. I kinda doubt this, but you could still try making the upper part’s hitbox thicker and test it. It’d also be better to use cylinders for better accuracy, just in case the ball grazes next to the hoop perfectly.

You could merge both scripts in one.

Also, i don’t recommend you use _G since it’s a bad practice, i recommend you use ModuleScript, that might be helpful.

_G isn’t inherently bad practice, it’s the way people use it which causes bad habits to form, especially in the case of conflicting code trying to use the same key. In the first place, if you have ModuleScripts hanging around, then yeah, you should probably just use those instead.

It’s because I’m using the Aero Game Framework from crazyman32 hehe

Hey, from your original code, if you’re trying to detect if the player has shot the ball into the hoop, ā€œOnTopā€ won’t work the best, for example if it just hit the top of the rim, and fell off then it’d still count as a score with the logic your implying, if this isnt the case please respond back, since I did not read all of it.

I basically kept the OnTop value true until it hit the Goal part, basically did it go through the net or did they score it. So it hits the rim or above the rim, I set the value to true, then if it goes any lower than that, I reset that value aka I set that value to false.

if Ball_HandlerPlayer and Config.Quarter.Value ~= "-" and Config.Quarter.Value ~= "H" and OnTopPartDebounce then
	OnTopPartDebounce = false
	--SET ONTOP VALUE TO TRUE
	Config.OnTop.Value = true
	--SET SHOTS ATTEMPTED
	ShotsAttempted.Value += 1
	--CHECK IF PLAYER ATTEMPTED 3 POINTER
	if ((XZ(HitRim.Position) - XZ(rootPos)).magnitude > 58) then
		ThreePointsAttempted.Value += 1
	end
	--SET REBOUND TO TRUE
	BallValues.Rebound.Value = true
	--SET SHOTCLOCK TO 17
	Config.SCTime.Value = 17
	customwait(1)
	--RESET DEBOUNCE
	OnTopPartDebounce = true
end

Thank you for explaining, i’ll keep that in mind in the future