soo basically im having a problem with this script:
-- services
local pathfinds = game:GetService("PathfindingService")
local path = pathfinds:CreatePath()
-- strings, raycastparams
local char = script.Parent
local raycastp = RaycastParams.new()
raycastp.FilterType = Enum.RaycastFilterType.Exclude
local maxdis = 30
local waypoints = workspace:WaitForChild("waypoints")
local distance
local movetoconnection
local state = "none"
local rpart = nil
local ray
local lastI
local echar
for i,v in pairs(char:GetChildren()) do
if v:IsA("BasePart") then
raycastp.FilterDescendantsInstances = {v}
end
end
local players = game.Players
local part = nil
local function createraycast(parts)
local ray = workspace:Raycast(char.Head.Position,( parts.Position - char.Head.Position ),raycastp)
return ray
end
local function patrol(wpoints,parts,distance,echar)
for i,vv in ipairs(wpoints) do
if state == "none" then
char.Humanoid.WalkSpeed = 8
char.Humanoid:MoveTo(vv.Position)
if state ~= "none" then
break
end
else
break
end
end
end
game.Players.PlayerAdded:Connect(function(plr)
if plr then
local char = plr.Character or plr.CharacterAdded:Wait()
echar = char
end
end)
while true do
wait()
if echar then
char.HumanoidRootPart:SetNetworkOwner(nil)
for _pn,parts in pairs(echar:GetChildren()) do
if parts:IsA("BasePart") then
ray = createraycast(parts)
echar.Humanoid.Died:Connect(function()
local othechar = echar
echar = nil
workspace:WaitForChild(othechar.Name)
if othechar then
echar = othechar
othechar = nil
end
end)
local distance = (char.HumanoidRootPart.Position - echar.HumanoidRootPart.Position).Magnitude
if distance <=2 then
echar.Humanoid:TakeDamage(100)
end
if ray then
local rpart = Instance.new("Part",workspace)
game.Debris:AddItem(rpart,.3)
rpart.CanCollide = false
rpart.Anchored = true
rpart.CanQuery = false
local midpoint = (char.Head.Position/2 + parts.Position/2)
rpart.CFrame = CFrame.new(midpoint,char.Head.Position)
print(ray.Instance)
rpart.Size = Vector3.new(.025,.025,ray.Distance)
distance = (char.HumanoidRootPart.Position - echar.HumanoidRootPart.Position).Magnitude
if ray.Instance == parts and distance < maxdis and echar.Humanoid.Health >0 then
maxdis = distance
state = "chasing"
char.Humanoid.WalkSpeed = 11
path:ComputeAsync(char.HumanoidRootPart.Position,echar.HumanoidRootPart.Position)
local wpoints = path:GetWaypoints()
for i,v in ipairs(wpoints) do
if distance <= maxdis then
char.Humanoid:MoveTo(v.Position)
else
print("DAHH BRO")
break
end
end
end
if ray.Instance ~= parts or distance >= maxdis then
maxdis = 30
state = "none"
path:ComputeAsync(char.HumanoidRootPart.Position,waypoints.aipart.Position)
local wpointss = path:GetWaypoints()
patrol(wpointss,parts,distance,echar)
end
if ray.Instance.Name == "Handle" then
ray.Instance.CanCollide = false
ray.Instance.CanQuery = false
end
end
end
end
end
end
the raycasts should be created at the same time, but for some reason it got a delay in raycast creation, making soo with more than 1 player even one of the player being next to the ai, it must wait to create all raycasts in one player to create in another one.
-- services
local pathfinds = game:GetService("PathfindingService")
local path = pathfinds:CreatePath()
local waypoints = workspace:WaitForChild("waypoints")
local maxdis = 30
-- raycast parameters
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
-- function to create raycast
local function createRaycast(origin, direction, filterDescendants)
return workspace:Raycast(origin, direction, raycastParams)
end
-- function to handle patrol
local function patrol(character, waypoints)
while true do
for _, waypoint in ipairs(waypoints) do
if (character.HumanoidRootPart.Position - waypoint.Position).Magnitude > 2 then
character.Humanoid:MoveTo(waypoint.Position)
wait(1) -- Adjust as needed
end
end
end
end
-- function to handle raycast detection
local function handleDetection(character, playerCharacter)
while true do
for _, part in ipairs(playerCharacter:GetChildren()) do
if part:IsA("BasePart") then
local ray = createRaycast(character.HumanoidRootPart.Position, (part.Position - character.HumanoidRootPart.Position), {part})
if ray then
-- Handle raycast hit
local distance = (character.HumanoidRootPart.Position - playerCharacter.HumanoidRootPart.Position).Magnitude
if distance <= maxdis then
-- Perform actions when within range
-- Example: Chase the player
path:ComputeAsync(character.HumanoidRootPart.Position, playerCharacter.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
character.Humanoid:MoveTo(waypoint.Position)
end
else
-- Perform actions when out of range
-- Example: Patrol waypoints
patrol(character, waypoints:GetChildren())
end
end
end
end
wait() -- Adjust as needed
end
end
-- Main loop
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
character.HumanoidRootPart:SetNetworkOwner(nil)
spawn(function()
handleDetection(character, character)
end)
end)
1- the ai character doesn’t move,
2- you set the player’s character’s network to nil, not the ai’s character
3- the player character name is also character just as the ai character name
4- the patrol uses while true do with a wait(), if your gonna use this script for anything i recommend using task.wait(), so it dont delay the rest of the script because of the while true
Okay. I’ve checked out each of those Issues and I tried again
-- services
local pathfinds = game:GetService("PathfindingService")
local waypoints = workspace:WaitForChild("waypoints")
local maxdis = 30
-- raycast parameters
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
-- function to create raycast
local function createRaycast(origin, direction, filterDescendants)
return workspace:Raycast(origin, direction, raycastParams)
end
-- function to handle patrol
local function patrol(character, waypoints)
while true do
for _, waypoint in ipairs(waypoints) do
if (character.HumanoidRootPart.Position - waypoint.Position).Magnitude > 2 then
character.Humanoid:MoveTo(waypoint.Position)
task.wait(1) -- Using task.wait() instead of wait()
end
end
end
end
-- function to handle raycast detection
local function handleDetection(character, playerCharacter)
while true do
for _, part in ipairs(playerCharacter:GetChildren()) do
if part:IsA("BasePart") then
local ray = createRaycast(character.HumanoidRootPart.Position, (part.Position - character.HumanoidRootPart.Position), {character})
if ray then
-- Handle raycast hit
local distance = (character.HumanoidRootPart.Position - playerCharacter.HumanoidRootPart.Position).Magnitude
if distance <= maxdis then
-- Perform actions when within range
-- Example: Chase the player
local path = pathfinds:CreatePath()
path:ComputeAsync(character.HumanoidRootPart.Position, playerCharacter.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
character.Humanoid:MoveTo(waypoint.Position)
end
else
-- Perform actions when out of range
-- Example: Patrol waypoints
patrol(character, waypoints:GetChildren())
end
end
end
end
task.wait() -- Using task.wait() instead of wait()
end
end
-- Main loop
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
character.HumanoidRootPart:SetNetworkOwner(nil) -- Corrected to set AI's network owner to nil
spawn(function()
handleDetection(character, character)
end)
end)
there is still this issue, you use the same name (character) to the ai’s character and to the player’s,
take a look at this script:
-- services
local pathfinds = game:GetService("PathfindingService")
local path = pathfinds:CreatePath()
local waypoints = workspace:WaitForChild("waypoints")
local maxdis = 30
-- raycast parameters
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
-- function to create raycast
local function createRaycast(origin, direction, filterDescendants)
return workspace:Raycast(origin, direction, raycastParams)
end
-- function to handle patrol
local function patrol(character, waypoints)
for _i, waypoint in ipairs(waypoints) do
if (character.HumanoidRootPart.Position - waypoint.Position).Magnitude > 2 then
character.Humanoid:MoveTo(waypoint.Position)
task.wait(1) -- Adjust as needed
print("helou")
end
end
end
-- function to handle raycast detection
local function handleDetection(character, playerCharacter)
while true do
for _, part in ipairs(playerCharacter:GetChildren()) do
if part:IsA("BasePart") then
local ray = createRaycast(character.HumanoidRootPart.Position, (part.Position - character.HumanoidRootPart.Position), {part})
if ray then
print(ray.Instance)
-- Handle raycast hit
local distance = (character.HumanoidRootPart.Position - playerCharacter.HumanoidRootPart.Position).Magnitude
if distance <= maxdis then
-- Perform actions when within range
-- Example: Chase the player
path:ComputeAsync(character.HumanoidRootPart.Position, playerCharacter.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
character.Humanoid:MoveTo(waypoint.Position)
end
else
-- Perform actions when out of range
-- Example: Patrol waypoints
patrol(character, waypoints:GetChildren())
end
end
end
end
wait() -- Adjust as needed
end
end
-- Main loop
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
script.Parent.HumanoidRootPart:SetNetworkOwner(nil)
spawn(function()
handleDetection(script.Parent, character)
end)
end)
i fixed some of the issues of the last one, but there still some like, the function handle wont be activated a second time because of the while true do making so the rest of the script (function to check if a new player is in game) dont work