Hi !
I’ve tried to recreate the AI of clash royale and it’s work but there is a lot of problem, the pathfinding doesn’t count other AI so they they pile up and get stuck, and other little problems, so I ask you to know how you would do this !
Thanks you for the feedback and have a nice day !
It would be better for everyone if you’d show us your code!
But forget about that.
Here are some simple steps and code to make the AI work.
NOTE: YOU MUST FIX AND MAKE IT WORK WITH YOUR GAME CAUSE I HAVE NO IDEA HOW YOUR GAME LOOKS LIKE!
It’s difficult to write the AI for a Clash of Clans-style game without more information about the gameplay mechanics and the specific behaviors you want the AI to exhibit. However, here is an example of how you could approach implementing AI for units in a Roblox game using decision trees:
function determineAction(unit)
-- check if the unit is being attacked by an enemy
if unit.IsUnderAttack then
-- if the unit is being attacked, prioritize defending
return "defend"
end
-- check if there are any enemy units in range
local enemyUnits = unit:GetEnemiesInRange()
if #enemyUnits > 0 then
-- if there are enemy units in range, prioritize attacking them
return "attack"
end
-- if there are no enemies in range and the unit is not being attacked, look for a target to attack
local target = findTarget(unit)
if target then
-- if a target was found, move towards it
return "move"
end
-- if no action was determined, return "idle"
return "idle"
end
-- function to find a target for a unit to attack
function findTarget(unit)
-- search for enemy units in a certain radius around the unit
local enemyUnits = unit:GetEnemiesInRange(unit.AttackRange)
-- if there are no enemy units in range, return nil
if #enemyUnits == 0 then
return nil
end
-- sort the enemy units by distance from the unit
table.sort(enemyUnits, function(a, b) return (a.Position - unit.Position).Magnitude < (b.Position - unit.Position).Magnitude end)
-- return the closest enemy unit
return enemyUnits[1]
end
-- function to execute the action determined by the AI
function executeAction(unit, action)
if action == "defend" then
-- code to defend against enemy attacks goes here
elseif action == "attack" then
-- code to attack enemy units goes here
elseif action == "move" then
-- code to move towards a target goes here
elseif action == "idle" then
-- code to idle goes here
end
end
-- example usage: determine and execute the best action for a unit
local action = determineAction(unit)
executeAction(unit, action)
This code defines three functions: determineAction
, which determines the best course of action for a unit based on its surroundings; findTarget
, which searches for a target for the unit to attack; and executeAction
, which executes the action determined by the AI.
The determineAction
function uses a series of conditions to determine the best course of action for the unit, based on whether it is being attacked, whether there are enemy units in range, and whether it has a target to attack. The findTarget
function searches for enemy units in a certain radius around the unit and returns the closest one. Finally, the executeAction
function executes the action determined by the AI, using a series of if
statements to determine which code to run.
This is just one example of how you could implement AI for units in a Roblox game. You may need to adjust the code or add additional functionality!
Now you are on your own. You can’t say u don’t understand since you already wanted to make this kind of AI for your game. You start small if you can’t make complex things. And I do not consider this complex, because I’ve made multiple AI systems in the past and never gave up.
If you need more help then I think It’s better for you to look at tutorials and start small!
Hi !
I have already make a function to find a target and I can show you the script :
local Character = script.Parent
local EnemyID = tostring((tonumber(Character.Name)-1)^2)
local EnemyTeam = workspace:WaitForChild("T"..EnemyID)
local Left = workspace:WaitForChild("Left"..EnemyID)
local LeftPosition = Left.Position
local Right = workspace:WaitForChild("Right"..EnemyID)
local RightPosition = Right.Position
local Middle = workspace:WaitForChild("Middle"..EnemyID)
local MiddlePosition = Middle.Position
local function FindTower()
local CharacterPosition = Character.PrimaryPart.Position
local LeftMagnitude = (CharacterPosition - LeftPosition).Magnitude
local RightMagnitude = (CharacterPosition - RightPosition).Magnitude
local MiddleMagnitude = (CharacterPosition - MiddlePosition).Magnitude
local LeftDestroyed = workspace:FindFirstChild("Left"..EnemyID)
local RightDestroyed = workspace:FindFirstChild("Right"..EnemyID)
if LeftDestroyed and RightDestroyed then
if LeftMagnitude < RightMagnitude then
return LeftPosition
else
return RightPosition
end
elseif LeftDestroyed and not RightDestroyed then
if LeftMagnitude < MiddleMagnitude then
return LeftPosition
else
return MiddlePosition
end
elseif not LeftDestroyed and RightDestroyed then
if RightMagnitude < MiddleMagnitude then
return RightPosition
else
return MiddlePosition
end
else
return MiddlePosition
end
end
local function FindTarget()
local CharacterPosition = Character.PrimaryPart.Position
local EnemiesMagnitude = {}
for v, Enemy in pairs(EnemyTeam:GetChildren()) do
local EnemyPosition = Enemy.PrimaryPart.Position
local EnemyMagnitude = (CharacterPosition - EnemyPosition).Magnitude
EnemiesMagnitude[Enemy] = EnemyMagnitude
end
local SmallestValue = math.huge
local SmallestKey
for Key, Value in pairs(EnemiesMagnitude) do
if Value < SmallestValue then
SmallestKey, SmallestValue = Key, Value
end
end
if SmallestValue <= 20 then
local SmallestKeyPosition = SmallestKey.PrimaryPart.Position
return SmallestKeyPosition
else
return FindTower()
end
end
This script return a position, and after that I was using the position to create a path with pathfinding but the problem is that the AI ignores other AI in the creating of the path !
To know that I go to 2nd waypoint of the waypoints that return and after that I refresh the target !
Sorry for my bad english …
Thanks you for the reply and have a nice day !
It’s a Clash Royale style !
This text will be blurred
I have a update it but I got the same problem, they pile up :
local PathfindingService = game:GetService("PathfindingService")
local Character = script.Parent
local Humanoid = Character.Humanoid
local EnemyID = tostring((tonumber(Character.Name)-1)^2)
local EnemyTeam = workspace:WaitForChild("T"..EnemyID)
local Left = workspace:WaitForChild("Left"..EnemyID)
local LeftPosition = Left.Position
local Right = workspace:WaitForChild("Right"..EnemyID)
local RightPosition = Right.Position
local Middle = workspace:WaitForChild("Middle"..EnemyID)
local MiddlePosition = Middle.Position
local AgentParameters = {Radius = 5, Height = 5, CanJump = false}
Character.Parent = workspace:WaitForChild("T"..Character.Name)
local function FindTarget()
local CharacterPosition = Character.PrimaryPart.Position
local EnemiesMagnitude = {}
for v, Enemy in pairs(EnemyTeam:GetChildren()) do
local EnemyPosition = Enemy.PrimaryPart.Position
local EnemyMagnitude = (CharacterPosition - EnemyPosition).Magnitude
EnemiesMagnitude[Enemy] = EnemyMagnitude
end
local SmallestValue = math.huge
local SmallestKey
for Key, Value in pairs(EnemiesMagnitude) do
if Value < SmallestValue then
SmallestKey, SmallestValue = Key, Value
end
end
if SmallestValue <= 20 then
local SmallestKeyPosition = SmallestKey.PrimaryPart.Position
return SmallestKeyPosition
else
local LeftMagnitude = (CharacterPosition - LeftPosition).Magnitude
local RightMagnitude = (CharacterPosition - RightPosition).Magnitude
local MiddleMagnitude = (CharacterPosition - MiddlePosition).Magnitude
local LeftDestroyed = workspace:FindFirstChild("Left"..EnemyID)
local RightDestroyed = workspace:FindFirstChild("Right"..EnemyID)
if LeftDestroyed and RightDestroyed then
if LeftMagnitude < RightMagnitude then
return LeftPosition
else
return RightPosition
end
elseif LeftDestroyed and not RightDestroyed then
if LeftMagnitude < MiddleMagnitude then
return LeftPosition
else
return MiddlePosition
end
elseif not LeftDestroyed and RightDestroyed then
if RightMagnitude < MiddleMagnitude then
return RightPosition
else
return MiddlePosition
end
else
return MiddlePosition
end
end
end
local function NextWaypoint()
local CharacterPosition = Character.PrimaryPart.Position
local Magnitude = (CharacterPosition - FindTarget()).Magnitude
if Magnitude > 5 then
local Path = PathfindingService:CreatePath(AgentParameters)
local Position = FindTarget()
local EndPosition = Vector3.new(Position.X, 10, Position.Z)
Path:ComputeAsync(CharacterPosition, EndPosition)
local Waypoints = Path:GetWaypoints()
if Waypoints[2].Position then
local TargetPosition = Waypoints[2].Position
return TargetPosition
end
else
return CharacterPosition
end
end
while wait() do
Humanoid:MoveTo(NextWaypoint())
end
Here a picture of the problem :
Thanks you for the reply and have a nice day !