Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How should I implement this code into my current script?

Asked by 4 years ago

Hello, I'm creating a game where you have to fight enemy NPC's that are trying to get to the center of your base. I want them to randomly spawn in a circle around your base.

More info at bottom | The full script is at the bottom also

I didn't know how to spawn NPC's randomly in a circle, so I asked on Reddit and someone was able to give this answer: I've tried to make it work, but I honestly don't know what I'm doing wrong.

origin = Vector3.new(0, 0, 0)

x = 200 

y = 15

p = part

model.HumanoidRootPart.CFrame = CFrame.new(origin + Vector3.new(math.random(-x, x), y, math.random(-x, x)))

What I've tried: I added the variables origin, x, y, and p and inserted the code at the bottom of the main enemy script, and nothing happened.

I looked around and noticed that on Line 47, there was:

Enemy.HumanoidRootPart.CFrame = workspace.Spawn.CFrame + Vector3.new(math.random(-4,4),0,math.random(-4,4))

which looked similar, so I tried replacing that with the aforementioned code, replacing model with Enemy. This time the NPC's wouldn't spawn.

Extra Info

Honestly, this is my first time scripting, and I need help.

This code comes from Bereza's Tower Defence Demo. To add to that, I know that I'm supposed to be familiar with scripts that we post here, but, without Bereza's help, I would be stuck figuring out how to make everything from scratch, while constantly watching tutorials.

Main Spawning Code: https://pastebin.com/sHFwm2QP

Explorer and Studio: https://imgur.com/a/xTazyFu

Thanks for reading this, and I hope you can help.

0
I would just like to say how amazing this is formatted. Most of the time people (including myself lol) don't format as good as this is even on our best formatted post! Upvoted :) superawesome113 112 — 4y
0
Thanks! Phoenix_Ravin 19 — 4y
0
Try Enemy:MoveTo(Vector3 value) and model:MoveTo(Vector3 value) OnaKat 444 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

I would recommend this.

local origin = Vector3.new(0, 0, 0)
local x = 200 
local y = 15
local p = part

--Other scripts that comprise stuff needed for the script below

model.HumanoidRootPart.CFrame = CFrame.new(origin + Vector3.new(math.random(-x, x), y, math.random(-x, x)))
Ad

Answer this question