Alright, so I have created an option to command my soldier to move where I click. However, SOMETIMES it doesn't work and it causes a tiny amount of lag. I need help fixing it so it works 100% of the time and making it more efficient.
So I start out with detecting if the mouse has clicked on a position. It checks if a mode value for clicktomove is true, and then proceeds.
-- "modes" is a value in a GUI.
modes.Changed:connect(function()
if modes.Value == "Follow" and plr.Character.Humanoid.Health > 0 then
local mode = "Follow" -- This changes the movement mode to "FollowCharacter"
workspace.MoveEvent:FireServer(mode,pos)
elseif modes.Value == "Move" and plr.Character.Humanoid.Health > 0 then
for _,tool in ipairs(plr.Character:GetChildren()) do
if not tool:IsA("Tool") then
toole = false
else
toole = true
end
mouse.Button1Down:connect(function()
if modes.Value == "Move" and plr.Character.Humanoid.Health > 0 and toole == false then
pos = mouse.Hit.p
local mode = "Move" -- This changes the mode to "FollowMouseClick"
workspace.MoveEvent:FireServer(mode,pos)
end
end)
end
end
end)
Once the mode for movement is detected, it fires a server event to a script in the ServerScriptService to manage.
-- when Detecting server event, it changes a the movement value in each individual NPC soldier on the player's team to the correct mode (followcharacter, clicktomove)
workspace.MoveEvent.OnServerEvent:connect(function(plr,mode,pos)
for _,v in ipairs(workspace:GetChildren())do
if mode == "Move" and v:FindFirstChild("Humanoid") and v:FindFirstChild("Configurations")and v.Configurations:FindFirstChild("FollowClickPos") and v.Configurations:FindFirstChild("FollowClicker") and v.Configurations:FindFirstChild("Follow") and v:FindFirstChild("Owner") and v.Owner.Value:IsA("Player") and v.Owner.Value == plr then -- if mode is "clicktomove"
v.Configurations.FollowClickPos.Value = pos -- Give all the soldiers the mouse's position for later reference.
v.Configurations.FollowClicker.Value = true
v.Configurations.Follow.Value = false
elseif mode == "Follow" and v:FindFirstChild("Humanoid") and v:FindFirstChild("Configurations")and v.Configurations:FindFirstChild("FollowClickPos") and v.Configurations:FindFirstChild("FollowClicker") and v.Configurations:FindFirstChild("Follow") and v:FindFirstChild("Owner") and v.Owner.Value:IsA("Player") and v.Owner.Value == plr then -- If mode is "follow"
v.Configurations.FollowClicker.Value = false
v.Configurations.Follow.Value = true
end
end
end)
Now for the final part, the movement in the NPC. When the NPC receives the changed value, it now either follows the character, or moves where you click.
PS, there is a positionvalue in the soldier to tell them where to move in clicktomove mode.
while wait(1) do
local target = findNearestTorso(script.Parent.Torso.Position)
if target then
Aggro(target)
script.Parent.Humanoid:MoveTo(target.Position)
elseif not target then
if configs.Follow.Value == true and owner.Value ~= nil and owner.Value.Character and owner.Value.Character:FindFirstChild("Torso") then -- P.S. The owner value is the player that commands the NPC soldier.
script.Parent.Humanoid:MoveTo(owner.Value.Character.Torso.Position) -- if in followcharacter mode, move humanoid to the torso.
script.Parent.Humanoid:MoveTo(owner.Value.Character.Torso.Position)
elseif configs.FollowClicker.Value == true and owner.Value ~= nil and owner.Value.Character and owner.Value.Character:FindFirstChild("Torso") then
script.Parent.Humanoid:MoveTo(configs.FollowClickPos.Value) -- if in clicktomove mode, move to the position of the mouse.
end
Aggro(target)
end
end
end
If anyone could detect any errors or tell me something that could be fixed, tell me! I tried to make this detailed as possible. Thanks!
