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