Ball's position always move to 0, 0, 0

So, I’m trying to make a script that spawns 3 red balls every 10 seconds in a random position
But for some reason the 3 balls always moves to the position 0, 0, 0 (im kinda new to scripting)
and i got no errors/warning too

Here’s the server script:

local folder = script.Parent
local positions = {Vector3.new(52.408, 2, 30.216), 
	Vector3.new(-46.65, 14.624, 360.818), 
	Vector3.new(-49.179, 19.008, 183.573), 
	Vector3.new(-55.496, 69.908, 50.952), 
	Vector3.new(46.464, 2.95, 108.617), 
	Vector3.new(-11.509, 1.668, 115.479), 
	Vector3.new(10.458, 2.95, 117.396)}

while true do
	wait(10)
	local redBall1pos = positions[math.random(1, #positions)]
	print(redBall1pos)
	folder.RedBall1.Position = Vector3.new(redBall1pos)
	local redBall2pos = positions[math.random(1, #positions)]
	print(redBall2pos)
	folder.RedBall2.Position = Vector3.new(redBall2pos)
	local redBall3pos = positions[math.random(1, #positions)]
	print(redBall3pos)
	folder.RedBall3.Position = Vector3.new(redBall3pos)
	
	for i,v in pairs(folder:GetChildren()) do
		if v:IsA("BasePart")  then
			v.Transparency = 0
			v.CanCollide = true
			break
		end
	end
	
	wait(10)
	for i,v in pairs(folder:GetChildren()) do
		if v:IsA("BasePart")  then
			v.Transparency = 1
			v.CanCollide = false
			break
		end
	end
end

I don’t see any noticable problems with this, except that you could possibly have two balls at the same position. I’ve always used Random.new() when I want something random, as I believe the seed changes per Random object created. Give this a go:

local folder = script.Parent

local positions

while true do
    wait(10)

    positions = {
        Vector3.new(52.408, 2, 30.216), 
        Vector3.new(-46.65, 14.624, 360.818), 
        Vector3.new(-49.179, 19.008, 183.573), 
        Vector3.new(-55.496, 69.908, 50.952), 
        Vector3.new(46.464, 2.95, 108.617), 
        Vector3.new(-11.509, 1.668, 115.479), 
        Vector3.new(10.458, 2.95, 117.396)
    }

    local redBall1pos = table.remove(positions, Random.new():NextInteger(1, #positions))
    print(redBall1pos)
    folder.RedBall1.Position = redBall1pos

    local redBall2pos = table.remove(positions, Random.new():NextInteger(1, #positions))
    print(redBall2pos)
    folder.RedBall2.Position = redBall2pos

    local redBall3pos = table.remove(positions, Random.new():NextInteger(1, #positions))
    print(redBall3pos)
    folder.RedBall3.Position = redBall3pos
	
    for i,v in pairs(folder:GetChildren()) do
        if v:IsA("BasePart")  then
            v.Transparency = 0
            v.CanCollide = true
            break
        end
    end
	
    wait(10)

    for i,v in pairs(folder:GetChildren()) do
        if v:IsA("BasePart")  then
            v.Transparency = 1
            v.CanCollide = false
            break
        end
    end
end

EDIT: You could also randomise the positions based on the confines of wherever you’re spawning them eventually too, hence there’d be no need for a positions array to pick randomly from. :slight_smile:

I don’t think the Random.new() thing will resolve the issue. For starters, math.random() doesn’t seem to require a new seed for it to be a different value, and mainly: 0,0,0 isn’t one of the possible positions for the ball to be moved to, so it doesn’t matter if it wasn’t being random.

The issue is that you are creating a new vector3 with a vector3, hence it treating it as nil and as such making the new vector a position at 0,0,0.

Whenever you did Vector3.new(redBall1pos), remove the Vector3.new bit and it should work


edit: Here is the corrected code:

local folder = script.Parent
local positions = {Vector3.new(52.408, 2, 30.216), 
	Vector3.new(-46.65, 14.624, 360.818), 
	Vector3.new(-49.179, 19.008, 183.573), 
	Vector3.new(-55.496, 69.908, 50.952), 
	Vector3.new(46.464, 2.95, 108.617), 
	Vector3.new(-11.509, 1.668, 115.479), 
	Vector3.new(10.458, 2.95, 117.396)}

while true do
	wait(10)
	local redBall1pos = positions[math.random(1, #positions)]
	print(redBall1pos)
	folder.RedBall1.Position = redBall1pos
	local redBall2pos = positions[math.random(1, #positions)]
	print(redBall2pos)
	folder.RedBall2.Position = redBall2pos
	local redBall3pos = positions[math.random(1, #positions)]
	print(redBall3pos)
	folder.RedBall3.Position = redBall3pos
	
	for i,v in pairs(folder:GetChildren()) do
		if v:IsA("BasePart")  then
			v.Transparency = 0
			v.CanCollide = true
			break
		end
	end
	
	wait(10)
	for i,v in pairs(folder:GetChildren()) do
		if v:IsA("BasePart")  then
			v.Transparency = 1
			v.CanCollide = false
			break
		end
	end
end
1 Like

Oop, I didn’t notice that at all, good catch haha.

I have had to face this issue before, and I went about it with the use of the Clone() function.
All you do is put the part you want to clone in the ReplicatedStorage folder, and tell the script to clone that part, then parent the clone to the workspace (or any specific folder).

Also, so far I understand, you don’t generate random coordinates, but you randomly select which preset coordinates the ball should be summoned to. If that is as you wished, then keep that what you have. If else, I suggest making a local variable for the X, Y, or Z position with math.random and input the minimum and maximum studs that they can be spawned in the clone function.


Code preview
local RepicatedStorage = game:GetService("ReplicatedStorage")

local ball = game.ReplicatedStorage:FindFirstChild("Ball")

while true do
	for i = 1, 3 do --repeat 3 times, because you want 3 balls to spawn
		local xPos = math.random(-50, 50) --define the coordinates in which the balls can be spawned
		local yPos = math.random (1, 10) --for height, make sure you input the minimum height as the CENTER height of the ball
		local zPos = math.random(-50, 50)
		local function spawnBalls(part) --create a ball cloning and spawning function
		part = RepicatedStorage.Ball --what part to clone?
		part:Clone(ball).Parent = workspace --where does the clone move to?
			ball.Position = Vector3.new(xPos, yPos, zPos) --input the randomly generated coordinates as the new ball position
		end
		spawnBalls() --execute the function
	end
	task.wait(10) --time until balls spawn again
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.