its my first time making an AI and the error i have came across is at the if "Victim == nil" my problem with it is that even if it finds a player it continues to go through the function even though the scripts says that the victim variable is not nil
wait(2)
local runService = game:GetService("RunService")
local Players = game:GetService("Players")
local humanoid = script.Parent
local root = humanoid.Parent.HumanoidRootPart
function findPlayer()
local playerList = Players:GetPlayers()
for i, player in pairs(playerList) do
print("finding player")
if (root.Position - player.Character.HumanoidRootPart.Position).Magnitude <= 5 then
print("found player")
return player
end
end
end
while true do
wait(.1)
local Victim
if Victim == nil then
Victim = findPlayer()
print("finding player")
else
print(Victim)
end
end

2 comments
thank you i didnt realise i did this
Its a common error, easy to miss when looking for why.