local homingStartTimes = {} ::HomingBaseballThink <- function() { for (local ball; ball = Entities.FindByClassname(ball, "tf_projectile_stun_ball");) { // Make sure the ball belongs to the player and hasn't hit anything yet if (ball.GetOwner() == self && NetProps.GetPropBool(ball, "m_bTouched") == false) { printl("Ball exists, checking homing..."); // Get the ball's unique entity ID to track homing time local ballID = ball.entindex(); // Guard clause in case ballID is invalid if (ballID == null) { continue; } // Check if this ball has a recorded homing start time if (homingStartTimes.find(ballID) == null) { homingStartTimes[ballID] = Time(); // Record the start time when the ball begins homing } // Calculate how long this ball has been homing if (homingStartTimes.find(ballID) != null) { local homingTimeElapsed = Time() - homingStartTimes[ballID]; // If the homing time exceeds the limit, stop homing if (homingTimeElapsed > homingDuration) { printl("Homing time exceeded. Ball no longer homes."); continue; // Skip further homing calculations for this ball } } else { // If somehow homingStartTimes[ballID] is missing, skip this ball continue; } // Get the ball's current position local ballPos = ball.GetOrigin(); // Find the closest enemy in the search radius local closestEnemy = null; local closestDistance = searchRadius; // Start with the maximum search radius for (local ent; ent = Entities.FindInSphere(ent, ballPos, searchRadius);) { // Check if the entity is an enemy and is alive if (ent.GetTeam() != self.GetTeam() && NetProps.GetPropInt(ent, "m_lifeState") == 0) { // Calculate distance between the ball and the enemy local enemyPos = ent.GetOrigin(); local distance = (enemyPos - ballPos).Length(); // If this enemy is closer than the previous closest one, update if (distance < closestDistance) { closestDistance = distance; closestEnemy = ent; } } } // If a target enemy was found, move the ball towards that enemy if (closestEnemy != null) { printl("Target found! Homing in on enemy..."); // Get the direction vector towards the target enemy local targetPos = closestEnemy.GetOrigin(); local direction = (targetPos - ballPos).Normalized(); // Normalized direction vector // Set the ball's velocity to move towards the enemy at the desired speed local newVelocity = direction * ballSpeed; ball.SetPhysVelocity(newVelocity); } } } return -1 } ::HomingNamespace <- { Cleanup = function() { // cleanup any persistent changes here // keep this at the end delete ::HomingNamespace } // mandatory events OnGameEvent_recalculate_holidays = function(_) { if (GetRoundState() == 3) Cleanup() } OnGameEvent_mvm_wave_complete = function(_) { Cleanup() } // add stored variables or your own events here // // e.g. // myvar = 123 // // OnGameEvent_player_death = function(params) // { ... } OnGameEvent_post_inventory_application = function(params) { local player = GetPlayerFromUserID(params.userid) player.ValidateScriptScope() local playerscope = player.GetScriptScope() local melee = GetItemInSlot(player, 2) local wep_id = NetProps.GetPropInt(melee, "m_AttributeManager.m_Item.m_iItemDefinitionIndex") if (wep_id == 44) { AddThinkToEnt(player, "HomingBaseballThink") } } }; __CollectGameEventCallbacks(HomingNamespace)