Help Regarding Soldier Npc That Follows Orders Given By User Using A Gui

Hey Dev’s,
I Am Working On An Military Game And I Want to Achieve This :

When Player Clicks A Button in A Gui Make 3 Soldier Npcs Should Follow Orders

The Buttons On GUI Include:-
1.Follow: All Three Npc Should Follow Player
2.Stay: All Three Npc Should Stop Following Player And Sit In Crouch Position
3.CoverFire: All Three Npc Should Fire At Enemy Npc While Following Player

here is a code which i have written but i am not sure:

-- References to GUI elements
local gui = script.Parent
local followButton = gui.FollowButton
local stayButton = gui.StayButton
local attackButton = gui.AttackButton

-- Reference to the friendly soldier NPC
local soldier = game.Workspace.Soldier

-- Function to make the soldier NPC follow the player
local function orderFollow()
    local playerCharacter = game.Players.LocalPlayer.Character
    if playerCharacter then
        local targetPosition = playerCharacter.HumanoidRootPart.Position
        soldier:MoveTo(targetPosition)
    end
end

-- Function to make the soldier NPC stay in place
local function orderStay()
    soldier.Humanoid:MoveTo(soldier.HumanoidRootPart.Position)
end

-- Function to make the soldier NPC attack a target
local function orderAttack()
    -- Implement the logic to make the soldier NPC attack a target
    -- You can use functions like :MoveTo() and :LoadAnimation() to create attack behavior

    -- Example:
    local target = game.Workspace.Enemy
    if target then
        soldier:MoveTo(target.HumanoidRootPart.Position)
    end
end

-- Connect button click events to corresponding functions
followButton.MouseButton1Click:Connect(orderFollow)
stayButton.MouseButton1Click:Connect(orderStay)
attackButton.MouseButton1Click:Connect(orderAttack)

Note:- I want all the three npc to follow the player using path finding and during cover fire they should attack all the enemy npc using the guns but I don’t know how to make them do so,

Plz Help

1 Like

You can find out how to use pathfinding here.

As for making the soldiers fire guns, that’s going to be a bit more complicated but you can contact me if you happen to come back to this forum and still need help.

1 Like