1 Like
Hey, can you share the script please?
1 Like
this may sound dumb, but i wanna keep my script private. but i can provide the functions. i am really private with my code.
Line 212: main()
Line 134:¨
if path ~= nil then
followPath(path)
task.wait(0.01) -- Wait a short interval before checking again
end
task.wait(0.01) --
Line 191:
main()
function main()
-- get closest node to police car and target
local startNode = getClosestNode(policeCar.Position, nodesFolder:GetChildren())
local endNode = getClosestNode(target.Position, nodesFolder:GetChildren())
-- run A* search algorithm to find path
local path = aStarSearch(startNode, endNode, nodesFolder:GetChildren())
-- follow path
if path ~= nil then
followPath(path)
task.wait(0.01) -- Wait a short interval before checking again
end
task.wait(0.01) -- Wait a short interval before checking again
end
function followPath(path)
local currentTargetIndex = 1
-- loop until target is reached
while currentTargetIndex <= #path do
-- get current target node
local currentTarget = path[currentTargetIndex]
-- Calculate Part2_Forward based on the average of all parts in the folder
-- Calculate the dot product between Part1's forward direction and the direction to Part2
-- Check if Part2 is within the field of view
local params = RaycastParams.new()
params.FilterDescendantsInstances = { script.Parent }
-- move police car towards target
local speed = 50 -- change this value to adjust the speed of the police car
local threshold = math.clamp(script.Parent.VehicleSeat.Velocity.Magnitude * 0.98314,0,math.huge) -- change this value to adjust the distance threshold for switching to the next target
local direction = (currentTarget.Position - policeCar.Position).Unit
local distance = getDistance(currentTarget.Position, policeCar.Position)
local distance2 = getDistance(target.Position, policeCar.Position)
--print(raycastResult)
local DetectionAngle = math.rad(105)
-- print(dotProduct, 'dotted')
-- print(threshold)
-- print(distance)
while distance > threshold do
task.wait(0.001)
--print(distance)
-- move towards target
-- Define the parts
-- Define the tweening options
currentTarget.BrickColor = BrickColor.new('Really red')
script.Parent.Node.Value = currentTarget
-- Create the tween
task.wait(0.001)
if path ~= nil then
task.wait(0.001) -- Wait a short interval before checking again
main()
script.Parent.Node.Value = currentTarget
end
task.wait(0.001) -- Wait a short interval before checking again
end
-- generate a new path once the police car reaches the current target node
-- move to next target
currentTargetIndex = currentTargetIndex + 1
end
end
-- main function to start the search and follow the path
-- call main function to start script
main()
end
The A*Star code is only hidden because its not causing the root
I don’t know why in Dev Forum, the path syntax is highlighted blue which could mean its already a function, but try change the path name into something else.
path --blue
pathAstar --not blue
path is not a mentioned syntax in studio LUA scripting.
You forgot to add wait/task.wait, add one under that line, it’s running too fast and overloading
- A_Star:134
14:46:54.483 Script ‘Workspace.Police_Cruiser_NPC.A_Star’, Line 193 - function followPath - Studio - A_Star:193
14:46:54.483 Script ‘Workspace.Police_Cruiser_NPC.A_Star’, Line 134 - function main - Studio - A_Star:134
14:46:54.483 Script ‘Workspace.Police_Cruiser_NPC.A_Star’, Line 193 - function followPath - Studio - A_Star:193
14:46:54.483 Script ‘Workspace.Police_Cruiser_NPC.A_Star’, Line 134 - function main - Studio - A_Star:134
14:46:54.483 Script ‘Workspace.Police_Cruiser_NPC.A_Star’, Line 214 - Studio - A_Star:214
14:46:54.483 Stack End - Studio,
-- helper function to follow a path
function main()
-- get closest node to police car and target
local startNode = getClosestNode(policeCar.Position, nodesFolder:GetChildren())
local endNode = getClosestNode(target.Position, nodesFolder:GetChildren())
-- run A* search algorithm to find path
local path = aStarSearch(startNode, endNode, nodesFolder:GetChildren())
-- follow path
if path ~= nil then
followPath(path)
task.wait(0.01) -- Wait a short interval before checking again
end
task.wait(0.01) -- Wait a short interval before checking again
end
function followPath(path)
local currentTargetIndex = 1
-- loop until target is reached
while currentTargetIndex <= #path do
-- get current target node
task.wait(0.001) -- Wait a short interval before checking again
local currentTarget = path[currentTargetIndex]
-- Calculate Part2_Forward based on the average of all parts in the folder
-- Calculate the dot product between Part1's forward direction and the direction to Part2
-- Check if Part2 is within the field of view
local params = RaycastParams.new()
params.FilterDescendantsInstances = { script.Parent }
-- move police car towards target
local speed = 50 -- change this value to adjust the speed of the police car
local threshold = math.clamp(script.Parent.VehicleSeat.Velocity.Magnitude * 0.98314,0,math.huge) -- change this value to adjust the distance threshold for switching to the next target
local direction = (currentTarget.Position - policeCar.Position).Unit
local distance = getDistance(currentTarget.Position, policeCar.Position)
local distance2 = getDistance(target.Position, policeCar.Position)
--print(raycastResult)
local DetectionAngle = math.rad(105)
-- print(dotProduct, 'dotted')
-- print(threshold)
-- print(distance)
while distance > threshold do
task.wait(0.001)
--print(distance)
-- move towards target
-- Define the parts
-- Define the tweening options
currentTarget.BrickColor = BrickColor.new('Really red')
script.Parent.Node.Value = currentTarget
-- Create the tween
task.wait(0.001)
if path ~= nil then
task.wait(0.001) -- Wait a short interval before checking again
main()
script.Parent.Node.Value = currentTarget
end
task.wait(0.001) -- Wait a short interval before checking again
end
-- generate a new path once the police car reaches the current target node
-- move to next target
currentTargetIndex = currentTargetIndex + 1
end
end
-- main function to start the search and follow the path
-- call main function to start script
main()
end
Well what now?