Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Trying to make a part move positions every 100 studs. It only does it once. What did I do wrong?

I was messing around with a script because I had recently just learned some basic functions and I wanted to make a randomized position for every couple seconds. I made this while true do loop and it won't loop. What did I do wrong?

The part spawns, then teleports to a random area within 100 studs, and stops. That's all. Why won't the script loop?

function partNew(color,transparency,position,name)
	local part = Instance.new("Part")
	part.Name = name
	part.BrickColor = color
	part.Transparency = transparency
	part.Reflectance = 0
	part.Anchored = true
	part.CanCollide = true
	part.Position = position
	part.Parent = workspace
	return part
end

local one = partNew(BrickColor.Random(),0.5,Vector3.new(1,1,1),"PartOne")
local randVec = Vector3.new(math.random(100), y, math.random(100))

while true do
	one.Position = randVec
	wait(1)
end


3 comments

0

did the script enconter any erors ?

0

The "randVec" should be inside the loop, so it will change it's value each run.

0

And, is the variable "y" defined?

1 answer

✓ Accepted answer

Hello, menacing_uzi!

You need to add 2 Vector3 so it correctly works, also the variable y is nil in your script

function partNew(color,transparency,position,name)
    local part = Instance.new("Part")
    part.Name = name
    part.BrickColor = color
    part.Transparency = transparency
    part.Reflectance = 0
    part.Anchored = true
    part.CanCollide = true
    part.Position = position
    part.Parent = workspace
    return part
end

local one = partNew(BrickColor.Random(),0.5,Vector3.new(1,1,1),"PartOne")
local randVec = Vector3.new(math.random(100), y, math.random(100))

while true do
    one.Position = one.Position + randVec -- here you were directly setting the position, instead of adding it
    wait(1)
end


EDIT: From what I understood from your question, if you want to teleport the part to a random position inside the 100 studs every second, you need to place the randVec inside the loop

function partNew(color,transparency,position,name)
    local part = Instance.new("Part")
    part.Name = name
    part.BrickColor = color
    part.Transparency = transparency
    part.Reflectance = 0
    part.Anchored = true
    part.CanCollide = true
    part.Position = position
    part.Parent = workspace
    return part
end

local one = partNew(BrickColor.Random(),0.5,Vector3.new(1,1,1),"PartOne")

while true do
    local randVec = Vector3.new(math.random(100), y, math.random(100))
    one.Position = randVec 
    wait(1)
end

2 comments

2

dang leamir on the grind

0

ty! worked

This discussion is preserved as it was. New answers, comments, and votes are closed.