Angel8365Join Date: 2010-04-26 Post Count: 101 |
I'm creating a RPG, in which I have enemies that have to be slain. I would like to have them walk around randomly within a certain area (30x30 possibly). I have a script that can be found below, which works great. The only issue if, after it is finished running, the enemy will teleport back to where it started, making it teleport back to where it originated from. Is there any way to make the loop repeat, looking smooth? If you have any suggestions on how to make this script look clean, like a patrol npc, please let me know and leave a comment. If you're good at scripting, I'd also love if it was possible to have an aggro range (as in games like world of warcraft and such) so that if a mob moves a certain amount of units from its spawn place (40 or 50 in my case) It would turn around and walk back to its spawn point, and begin patrolling again. IDK if this is even possible, but I'd be ectastic if I could get my hands on a scipt like this. My game I've described will be on my profile if you need it for reference or whatever.
h = script.Parent.Enemy
t = script.Parent.Torso
while true do
local p = Instance.new("Part")
p.CanCollide = false
p.Transparency = 1
p.Anchored = true
p.Position = t.Position + Vector3.new(math.random(-30,30),0,math.random(-30,30))
p.Parent = script.Parent
h:MoveTo(Vector3.new(p.Position.X,p.Position.Y,p.Position.Z),p)
wait(math.random(2,3))
p:remove()
end
--Please don't reply if you're going to be one of those people who just tell me to "go learn and script",I came here for help and I'd love anyone who gave that to me <3 :D.
|
|
|
Angel8365Join Date: 2010-04-26 Post Count: 101 |
If I didn't need to have an aggro range,is there anyway i could just have them patrol around the map? Like go to a random place 30 units away, and just keep repeating? The biggest issue I had with the original script was they would randomly teleport away, even if you were in the middle of fighting them. I am not good at scripting, I usually just tweak them. Would something like the script below (or a working version of it) be able to accomplish that?
h = script.Parent.Enemy
t = script.Parent.Torso
while true do
local p = Instance.new("Part")
p.CanCollide = false
p.Transparency = 1
p.Anchored = true
p.Position = t.Position + Vector3.new(math.random(-30,30),0,math.random(-30,30))
p.Parent = script.Parent
h:MoveTo(Vector3.new(p.Position.X,p.Position.Y,p.Position.Z),p)
local p = remove ("Part")
end |
|
|
Use pathfinding, it can be interrupted:
http://wiki.roblox.com/index.php?title=Pathfinding
Also p:Destroy() instead of p:Remove(), as remove is deprecated.
|
|
Angel8365Join Date: 2010-04-26 Post Count: 101 |
As previously stated, I don't know how to script. I'm unfamiliar with Pathfinding, but it seems like what I'm looking for. Can you give me an example script of a random wandering path? |
|
|
function moveToPoint(pointName, AI)
local path = pathfindingService:ComputeRawPathAsync(AI.Torso.Position, pointName, 500)
for i, point in pairs(path:GetPointCoordinates()) do
AI.Humanoid:MoveTo(point)
repeat
local distance = (point - AI.Torso.Position).magnitude
wait()
until distance < 3
end
print("MovedToPoint "..pointName)
return "Done"
end
local t = script.Parent.Torso
local h = script.Parent.Enemy
while wait() do
local ranPoint = t.Position + Vector3.new(math.random(-30, 30), 0, math.random(-30, 30)
moveToPoint(ranPoint, script.Parent)
end
|
|
|
Whoops:
function moveToPoint(pointName, AI)
local path = pathfindingService:ComputeRawPathAsync(AI.Torso.Position, pointName, 500)
for i, point in pairs(path:GetPointCoordinates()) do
AI.Humanoid:MoveTo(point)
repeat
local distance = (point - AI.Torso.Position).magnitude
wait()
until distance < 3
end
print("MovedToPoint "..pointName)
return "Done"
end
local t = script.Parent.Torso
while wait() do
local ranPoint = t.Position + Vector3.new(math.random(-30, 30), 0, math.random(-30, 30))
moveToPoint(ranPoint, script.Parent)
end
|
|
Angel8365Join Date: 2010-04-26 Post Count: 101 |
Did I mess something up? When I inserted the script into my npc "Freelancer" all it did was make him walk in place. I tried switching the "AI" to enemy and Freelancer after to see if it was a naming issue, but it wasn't. How do i stop him from walking in place, because it looks like he would be patrolling if he actually moved. |
|
|
Hmm, try adding print(ranPoint) after the line local ranPoint = ...
and seeing if it prints the same thing.
|
|
Angel8365Join Date: 2010-04-26 Post Count: 101 |
function moveToPoint(pointName, Freelancer)
local path = pathfindingService:ComputeRawPathAsync(Freelancer.Torso.Position, pointName, 500)
for i, point in pairs(path:GetPointCoordinates()) do
AI.Humanoid:MoveTo(point)
repeat
local distance = (point - Freelancer.Torso.Position).magnitude
wait()
until distance < 3
end
print("MovedToPoint "..pointName)
return "Done"
end
local t = script.Parent.Torso
while wait() do
local ranPoint = t.Position + Vector3.new(math.random(-30, 30), 0, math.random(-30, 30))
print(ranPoint)
moveToPoint(ranPoint, script.Parent)
end
It looks like this now, but he is still walking in place. |
|
|
What does the output say though?
(press f9 if you are in a server, or just look at the error output in test mode)
|
|
Angel8365Join Date: 2010-04-26 Post Count: 101 |
On a positive note though, when approached it appears that the pathing is dropped and he directly targets the player. :D. |
|
Angel8365Join Date: 2010-04-26 Post Count: 101 |
Error output:
13:06:37.118 - Auto-Saving...
Tool Giver
ServerSocialScript Loaded
16.7943764, 2.95746827, -51.7700729
13:06:42.719 - Workspace.Freelancer.Script:2: attempt to index global 'pathfindingService' (a nil value)
13:06:42.719 - Stack Begin
13:06:42.719 - Script 'Workspace.Freelancer.Script', Line 2 - global moveToPoint
13:06:42.720 - Script 'Workspace.Freelancer.Script', Line 19
13:06:42.720 - Stack End
13:06:42.754 - Argument 1 missing or nil
13:06:42.754 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:06:42.755 - Script 'Workspace.Freelancer.Drop', Line 14
13:06:42.755 - Stack End
-23.2056236, 2.95746827, -17.7700729
13:06:52.728 - Workspace.Freelancer.Script:2: attempt to index global 'pathfindingService' (a nil value)
13:06:52.728 - Stack Begin
13:06:52.729 - Script 'Workspace.Freelancer.Script', Line 2 - global moveToPoint
13:06:52.729 - Script 'Workspace.Freelancer.Script', Line 19
13:06:52.729 - Stack End
13:06:52.756 - Argument 1 missing or nil
13:06:52.756 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:06:52.756 - Script 'Workspace.Freelancer.Drop', Line 14
13:06:52.757 - Stack End
13:07:02.787 - Workspace.Freelancer.Respawn:13: attempt to index field 'Parent' (a nil value)
13:07:02.787 - Stack Begin
13:07:02.789 - Script 'Workspace.Freelancer.Respawn', Line 13
13:07:02.789 - Stack End
2.7943759, 2.95746827, -67.7700729
13:07:02.790 - Workspace.Freelancer.Script:2: attempt to index global 'pathfindingService' (a nil value)
13:07:02.790 - Stack Begin
13:07:02.790 - Script 'Workspace.Freelancer.Script', Line 2 - global moveToPoint
13:07:02.791 - Script 'Workspace.Freelancer.Script', Line 19
13:07:02.791 - Stack End
13:07:02.791 - Argument 1 missing or nil
13:07:02.791 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:07:02.792 - Script 'Workspace.Freelancer.Drop', Line 14
13:07:02.792 - Stack End
13:07:12.824 - Workspace.Freelancer.Respawn:13: attempt to index field 'Parent' (a nil value)
13:07:12.824 - Stack Begin
13:07:12.825 - Script 'Workspace.Freelancer.Respawn', Line 13
13:07:12.825 - Stack End
8.79437637, 2.95746827, -52.7700729
13:07:12.825 - Workspace.Freelancer.Script:2: attempt to index global 'pathfindingService' (a nil value)
13:07:12.826 - Stack Begin
13:07:12.826 - Script 'Workspace.Freelancer.Script', Line 2 - global moveToPoint
13:07:12.827 - Script 'Workspace.Freelancer.Script', Line 19
13:07:12.827 - Stack End
13:07:12.827 - Argument 1 missing or nil
13:07:12.828 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:07:12.828 - Script 'Workspace.Freelancer.Drop', Line 14
13:07:12.828 - Stack End
13:07:22.814 - Workspace.Freelancer.Respawn:13: attempt to index field 'Parent' (a nil value)
13:07:22.815 - Stack Begin
13:07:22.815 - Script 'Workspace.Freelancer.Respawn', Line 13
13:07:22.816 - Stack End
-10.2056236, 2.95746827, -41.7700729
13:07:22.817 - Workspace.Freelancer.Script:2: attempt to index global 'pathfindingService' (a nil value)
13:07:22.818 - Stack Begin
13:07:22.818 - Script 'Workspace.Freelancer.Script', Line 2 - global moveToPoint
13:07:22.819 - Script 'Workspace.Freelancer.Script', Line 19
13:07:22.820 - Stack End
13:07:22.876 - Argument 1 missing or nil
13:07:22.876 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:07:22.877 - Script 'Workspace.Freelancer.Drop', Line 14
13:07:22.878 - Stack End |
|
|
Oh, here's the issue, you changed 'AI' to 'Freelancer' but forgot to change it in one place:
function moveToPoint(pointName, Freelancer)
local path = pathfindingService:ComputeRawPathAsync(Freelancer.Torso.Position, pointName, 500)
for i, point in pairs(path:GetPointCoordinates()) do
Freelancer.Humanoid:MoveTo(point) -- here
repeat
local distance = (point - Freelancer.Torso.Position).magnitude
wait()
until distance < 3
end
print("MovedToPoint "..pointName)
return "Done"
end
|
|
|
Anddddd I forgot some variables because I copied this from a script of mine, give me a second XD
|
|
|
local pathfindService = game:GetService('PathfindingService')
function moveToPoint(pointName, Freelancer)
local path = pathfindingService:ComputeRawPathAsync(Freelancer.Torso.Position, pointName, 500)
for i, point in pairs(path:GetPointCoordinates()) do
Freelancer.Humanoid:MoveTo(point) -- here
repeat
local distance = (point - Freelancer.Torso.Position).magnitude
wait()
until distance < 3
end
print("MovedToPoint "..pointName)
return "Done"
end
|
|
Angel8365Join Date: 2010-04-26 Post Count: 101 |
I made the change, but I'm still getting an error:
13:12:37.964 - Auto-Saving...
Tool Giver
ServerSocialScript Loaded
29.7943764, 2.95746827, -67.7700729
13:12:43.811 - Workspace.Freelancer.Script:2: attempt to index global 'pathfindingService' (a nil value)
13:12:43.811 - Stack Begin
13:12:43.812 - Script 'Workspace.Freelancer.Script', Line 2 - global moveToPoint
13:12:43.812 - Script 'Workspace.Freelancer.Script', Line 19
13:12:43.812 - Stack End
13:12:43.857 - Argument 1 missing or nil
13:12:43.857 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:12:43.857 - Script 'Workspace.Freelancer.Drop', Line 14
13:12:43.858 - Stack End
21.7943764, 2.95746827, -47.7700729
13:12:53.657 - Workspace.Freelancer.Script:2: attempt to index global 'pathfindingService' (a nil value)
13:12:53.658 - Stack Begin
13:12:53.658 - Script 'Workspace.Freelancer.Script', Line 2 - global moveToPoint
13:12:53.658 - Script 'Workspace.Freelancer.Script', Line 19
13:12:53.659 - Stack End
13:12:53.740 - Workspace.Freelancer.Drop:4: attempt to index a nil value
13:12:53.741 - Stack Begin
13:12:53.741 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:12:53.742 - Script 'Workspace.Freelancer.Drop', Line 14
13:12:53.743 - Stack End
13:13:03.690 - Workspace.Freelancer.Respawn:13: attempt to index field 'Parent' (a nil value)
13:13:03.691 - Stack Begin
13:13:03.691 - Script 'Workspace.Freelancer.Respawn', Line 13
13:13:03.692 - Stack End
7.7943759, 2.95746827, -64.7700729
13:13:03.693 - Workspace.Freelancer.Script:2: attempt to index global 'pathfindingService' (a nil value)
13:13:03.693 - Stack Begin
13:13:03.694 - Script 'Workspace.Freelancer.Script', Line 2 - global moveToPoint
13:13:03.694 - Script 'Workspace.Freelancer.Script', Line 19
13:13:03.695 - Stack End
13:13:03.765 - Argument 1 missing or nil
13:13:03.766 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:13:03.767 - Script 'Workspace.Freelancer.Drop', Line 14
13:13:03.767 - Stack End
13:13:13.845 - Workspace.Freelancer.Respawn:13: attempt to index field 'Parent' (a nil value)
13:13:13.846 - Stack Begin
13:13:13.846 - Script 'Workspace.Freelancer.Respawn', Line 13
13:13:13.847 - Stack End
6.7943759, 2.95746827, -34.7700729
13:13:13.923 - Workspace.Freelancer.Script:2: attempt to index global 'pathfindingService' (a nil value)
13:13:13.923 - Stack Begin
13:13:13.924 - Script 'Workspace.Freelancer.Script', Line 2 - global moveToPoint
13:13:13.924 - Script 'Workspace.Freelancer.Script', Line 19
13:13:13.925 - Stack End
13:13:13.980 - Argument 1 missing or nil
13:13:13.980 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:13:13.981 - Script 'Workspace.Freelancer.Drop', Line 14
13:13:13.982 - Stack End |
|
|
local pathfindingService = game:GetService('PathfindingService')
God my spelling is bad today, sorry
|
|
Angel8365Join Date: 2010-04-26 Post Count: 101 |
BTW Thanks a lot for the help, but I'm just going to give you everything so far, because I don't know where to put in that last edit.
previous script:
function moveToPoint(pointName, Freelancer)
local path = pathfindingService:ComputeRawPathAsync(Freelancer.Torso.Position, pointName, 500)
for i, point in pairs(path:GetPointCoordinates()) do
Freelancer.Humanoid:MoveTo(point) -- here
repeat
local distance = (point - Freelancer.Torso.Position).magnitude
wait()
until distance < 3
end
print("MovedToPoint "..pointName)
return "Done"
end
local t = script.Parent.Torso
while wait() do
local ranPoint = t.Position + Vector3.new(math.random(-30, 30), 0, math.random(-30, 30))
print(ranPoint)
moveToPoint(ranPoint, script.Parent)
end
edit:
local pathfindingService = game:GetService('PathfindingService')
so could you just like smash those two together and make it work? :>? <3 |
|
|
local pathfindingService = game:GetService('PathfindingService')
function moveToPoint(pointName, Freelancer)
local path = pathfindingService:ComputeRawPathAsync(Freelancer.Torso.Position, pointName, 500)
for i, point in pairs(path:GetPointCoordinates()) do
Freelancer.Humanoid:MoveTo(point) -- here
repeat
local distance = (point - Freelancer.Torso.Position).magnitude
wait()
until distance < 3
end
print("MovedToPoint "..pointName)
return "Done"
end
local t = script.Parent.Torso
while wait() do
local ranPoint = t.Position + Vector3.new(math.random(-30, 30), 0, math.random(-30, 30))
print(ranPoint)
moveToPoint(ranPoint, script.Parent)
end
|
|
Angel8365Join Date: 2010-04-26 Post Count: 101 |
different error now
13:27:21.821 - Auto-Saving...
Tool Giver
ServerSocialScript Loaded
14.7943764, 2.95746827, -57.7700729
13:27:27.328 - Humanoid is not a valid member of Model
13:27:27.328 - Script 'Workspace.Freelancer.Script', Line 6 - global moveToPoint
13:27:27.329 - Script 'Workspace.Freelancer.Script', Line 22
13:27:27.329 - Stack End
13:27:27.336 - Argument 1 missing or nil
13:27:27.336 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:27:27.337 - Script 'Workspace.Freelancer.Drop', Line 14
13:27:27.337 - Stack End
16.7943764, 2.95746827, -51.7700729
13:27:37.061 - Humanoid is not a valid member of Model
13:27:37.061 - Script 'Workspace.Freelancer.Script', Line 6 - global moveToPoint
13:27:37.061 - Script 'Workspace.Freelancer.Script', Line 22
13:27:37.062 - Stack End
13:27:37.133 - Argument 1 missing or nil
13:27:37.134 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:27:37.134 - Script 'Workspace.Freelancer.Drop', Line 14
13:27:37.134 - Stack End |
|
Angel8365Join Date: 2010-04-26 Post Count: 101 |
Changed humanoid to enemy now im getting this
Tool Giver
ServerSocialScript Loaded
-18.2056236, 2.95746827, -22.7700729
13:31:01.419 - Argument 1 missing or nil
13:31:01.419 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:31:01.419 - Script 'Workspace.Freelancer.Drop', Line 14
13:31:01.420 - Stack End
16.7943764, 2.95746827, -47.7700729
13:31:11.341 - Workspace.Freelancer.Drop:4: attempt to index a nil value
13:31:11.341 - Stack Begin
13:31:11.342 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:31:11.342 - Script 'Workspace.Freelancer.Drop', Line 14
13:31:11.342 - Stack End
13:31:21.301 - Workspace.Freelancer.Respawn:13: attempt to index field 'Parent' (a nil value)
13:31:21.302 - Stack Begin
13:31:21.302 - Script 'Workspace.Freelancer.Respawn', Line 13
13:31:21.302 - Stack End
0.794375896, 2.95746827, -66.7700729
13:31:21.379 - Argument 1 missing or nil
13:31:21.379 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:31:21.380 - Script 'Workspace.Freelancer.Drop', Line 14
13:31:21.380 - Stack End
13:31:22.754 - Workspace.Freelancer.Script:12: attempt to concatenate local 'pointName' (a userdata value)
13:31:22.754 - Stack Begin
13:31:22.755 - Script 'Workspace.Freelancer.Script', Line 12 - global moveToPoint
13:31:22.755 - Script 'Workspace.Freelancer.Script', Line 22
13:31:22.756 - Stack End
13:31:31.338 - Workspace.Freelancer.Respawn:13: attempt to index field 'Parent' (a nil value)
13:31:31.339 - Stack Begin
13:31:31.339 - Script 'Workspace.Freelancer.Respawn', Line 13
13:31:31.340 - Stack End
2.7943759, 2.95562649, -31.7700729
13:31:31.431 - Argument 1 missing or nil
13:31:31.431 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:31:31.432 - Script 'Workspace.Freelancer.Drop', Line 14
13:31:31.432 - Stack End
13:31:32.490 - Workspace.Freelancer.Script:12: attempt to concatenate local 'pointName' (a userdata value)
13:31:32.490 - Stack Begin
13:31:32.491 - Script 'Workspace.Freelancer.Script', Line 12 - global moveToPoint
13:31:32.491 - Script 'Workspace.Freelancer.Script', Line 22
13:31:32.492 - Stack End
13:31:41.374 - Workspace.Freelancer.Respawn:13: attempt to index field 'Parent' (a nil value)
13:31:41.375 - Stack Begin
13:31:41.375 - Script 'Workspace.Freelancer.Respawn', Line 13
13:31:41.375 - Stack End
-9.20562363, 2.95647812, -30.7700729
13:31:41.474 - Argument 1 missing or nil
13:31:41.475 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:31:41.475 - Script 'Workspace.Freelancer.Drop', Line 14
13:31:41.476 - Stack End |
|
|
From what I can see only 1 of those errors is a problem with this script:
local pathfindingService = game:GetService('PathfindingService')
function moveToPoint(pointName, Freelancer)
local path = pathfindingService:ComputeRawPathAsync(Freelancer.Torso.Position, pointName, 500)
for i, point in pairs(path:GetPointCoordinates()) do
Freelancer.Humanoid:MoveTo(point) -- here
repeat
local distance = (point - Freelancer.Torso.Position).magnitude
wait()
until distance < 3
end
return "Done"
end
local t = script.Parent.Torso
while wait() do
local ranPoint = t.Position + Vector3.new(math.random(-30, 30), 0, math.random(-30, 30))
print(ranPoint)
moveToPoint(ranPoint, script.Parent)
end
|
|
Angel8365Join Date: 2010-04-26 Post Count: 101 |
It's working, but he still randomly teleports back to the start.
13:36:33.467 - Auto-Saving...
Tool Giver
ServerSocialScript Loaded
-98.4055862, 2.94822145, 105.029884
13:36:38.969 - Argument 1 missing or nil
13:36:38.970 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:36:38.970 - Script 'Workspace.Freelancer.Drop', Line 14
13:36:38.970 - Stack End
-85.2311707, 3.00003529, 100.930748
-111.265846, 2.94117594, 129.847076
-96.8016281, 2.94117594, 156.478806
-99.6192093, 2.94116664, 181.029648
-72.0023575, 2.94117594, 183.022003
-121.405586, 2.94822145, 114.029884
13:36:48.830 - Argument 1 missing or nil
13:36:48.831 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:36:48.832 - Script 'Workspace.Freelancer.Drop', Line 14
13:36:48.832 - Stack End
-117.832787, 3.00003529, 104.163872
-143.746063, 3.00003529, 134.108002
-149.803162, 3.00003529, 130.533524
-153.582016, 3.00003529, 148.006912
-171.953415, 2.9411819, 155.530685
-170.938904, 2.94117594, 144.604889
-177.970963, 3.00003529, 168.441315
13:36:58.725 - Workspace.Freelancer.Respawn:13: attempt to index field 'Parent' (a nil value)
13:36:58.726 - Stack Begin
13:36:58.726 - Script 'Workspace.Freelancer.Respawn', Line 13
13:36:58.726 - Stack End
-137.405579, 2.94822145, 140.029877
13:36:58.811 - Argument 1 missing or nil
13:36:58.811 - Script 'Workspace.Freelancer.Drop', Line 4 - global Drop
13:36:58.812 - Script 'Workspace.Freelancer.Drop', Line 14
13:36:58.812 - Stack End
-148.656387, 2.94117594, 137.567795 |
|
|
Try disabling the scripts called Drop and Respawn inside the AI (click on the script then disabled in it's properties) and see what happens.
|
|