What would be the best method to make NPCs in a basketball game?

Now Im currently attempting the alpha beta pruning algorithm which is commonly used in chess i could have the the court broken down into a grid system but this is only in a theoretical state. So if any of you have any suggestions for Defensive and Offensive Ai for basketball it would be deeply appreciated. Ive tried looking for the code that helped run the npcs on famous basketball games like 2k and NBA Live but ive come up dry so ill take what i can get now.

Right now the npcs just operate off positions that i set according to plays that i made up myself but that is rather inefficient and predictable the defensive Npcs use Pathfinding to get to the position between the offensive opponent and the goal

The NPCs

local RunService = game:GetService("RunService")
local HB = RunService.Heartbeat

local module = {}

module.PathFind = function(OppositeTeam ,Team, ActualPlayer)
    while script.Parent.Configuration.Possesion.Value == false do
    local PathfindingService = game:GetService("PathfindingService")
    local Humanoids = {}
    local Parents = {}
    local Defense = {}
    for OtherTeam, Player in pairs(OppositeTeam) do
        if Player:IsA"Humanoid" then
            if Player.Parent ~= "1" then
                table.insert(Defense, Player.Parent.HumanoidRootPart)
            end
        end
    end
    for TheTeam, Player in pairs(Team) do
        if Player:IsA"Humanoid" then
            if Player.Parent.Name ~= ActualPlayer then
                print(ActualPlayer)
                table.insert(Humanoids, Player)
                table.insert(Parents, Player.Parent)
            end
        end
    end
    local Path = PathfindingService:CreatePath()
    for  OT = 1, #Humanoids do
        local Character = Defense[OT]
        local Goal = Character.Position + (script.Parent.Configuration.Goal.Value.Position - Character.Position).Unit * 5
            Path:ComputeAsync(Parents[OT].HumanoidRootPart.Position, Goal)      
            local points = Path:GetWaypoints()
        if Path.Status == Enum.PathStatus.Success then
            for i, marker in pairs(points) do
            Humanoids[OT]:MoveTo(marker.Position)
                if marker.Action == Enum.PathWaypointAction.Jump then
                    Humanoids[OT].Jump = true
                end
            end
        end
    end
    HB:Wait()
    end
end
3 Likes

I would try to sit down and watch some basketball to see what real players do. Learn the positioning when different players have the ball.
For example, guy has ball next to net. Guy ain’t teammate. Rush him or something (I’m not basketball expert, but if I saw a guy next to my net, I’ll make a jump to block a score).
See how people position themselves in formations on defensive and offensive. Predictable AI is good AI. Not too predictable and just the right predictable is best. When I say this I mean that AI should be logical. They should know how to react to certain situations accordingly.
So really, you can draw inspiration from real basketball players to see how they would react in those logical situations.
Also you wouldn’t need pathfinding as there no obstacles in a basketball court. I would also make the Npcs noncollidable with each other.

1 Like

I like this approach but there are a few things wrong the Pathfinding is so they can stay between the goal and their offensive opponent or run plays when on offense.

This is the thing im trying to figure out but ill try to script it raw instead of take a shortcut

The noncollidable plan wont work either i cant have nba players phasing through each other lol.

Outside of the pathfinding, this may need:

  • defined list of game events
  • defined list of NPC actions
  • defined map of game events to NPC actions
  • NPC AI function for each event
  • Connection to each event to fire corresponding function
  • defined Roblox Lua condition that triggers each game event

You’ll need to define what all of those are for your game system.

Game events might include things like clock started, fowl, penalty shot, player/NPC received the ball, scored, ball is on TeamA’s side of court, etc.

NPC actions might include things like catch, pass, shoot, dribble, move to offense side of court, etc.

How you do all this and how much work is involved is up to you, what kind of results do you want? It get crazy complex, which is why you need to pre-define itemize lists as suggested.

What makes this your game instead of someone else’s is that all of this hinges on how you decide to use Roblox Lua, Roblox game instance state, and events to determine how and what triggers one of your own game events.

You may also want to write a coroutine.wrap that has the NPC AI do some additional state checking on its own for each NPC (maybe every 5 or 10 heartbeats?) to determine NPC actions in addition to the event system.

Think of pathfinding to a goal position as just one possible action.

Corrections and critics welcome.

1 Like

Should add that behind-the-back dribble is sweet, already looks like a great start to the game.

1 Like

… and if the NPC actions are a layer abstracted from the events, then the offensive and defensive tactics for the AI are layer abstracted above NPC actions.

So, define a list of offensive tactic states and another for defensive tactic states. Then define a map of those states to NPC actions.

One offensive state might be “rush the net”, another might be “break away player”. Once you have defined that and have also defined which game states or events (and what triggers the events) should be mapped to which tactics, then you can script something functional.

1 Like

Damn I like this thorough response I appreciate it no lie

I was experimenting with ant colony optimization a while back for use in zombie swarm intelligence and it actually worked really well.

You could try implementing this while calculating open “passing” positions for NPCs based on the diagonals from NPC that currently has possession of the ball. I would also focus on creating a state-based system for both the team and the individual NPCs; defending, attacking — gaurding, traveling, ect. Overall I think it’s doable, you will just need a lot of modular features for it to work fluidly.

1 Like

You’re welcome! You might never feel like you’re finished this game. This kind of AI programmer gets more and more complicated the more you think about it. There’s a reason AAA games have lots of people working on them.

1 Like

Yea thinks for that ill do some studying on that algorithm and for passing im just thinking about using interpolation, as for

im thinking about how . id work it in i just dont know where to start