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

How come math.randomseed doesn't work for Vector3 Position?

So I tried to generate random trees. However, the trees stack up on one position.

math.randomseed(tick())
tree = game.ReplicatedStorage["Pine Tree"]

function randomTrees(pos)
	for i = 1, 10 do
		local treeClone = tree:Clone()
		treeClone.Parent = game.Workspace
		treeClone:MoveTo(pos)
		for _, child in pairs(treeClone:GetChildren()) do
			child.Anchored = true
		end
	end
end
randomTrees(Vector3.new(math.random(-256, 256), 0, math.random(-256, 256)))

I even used math.randomseed. What's wrong?

1 comment

1 answer

✓ Accepted answer

You're only giving one position as an argument, therefore all the trees are being moved to that one position and stacking on top of each other.

To fix your script, you should set the position inside of your function so that a new position is generated each time you create a new tree.


math.randomseed(tick())
tree = game.ReplicatedStorage["Pine Tree"]

function randomTrees()
    for i = 1, 10 do
        local treeClone = tree:Clone()
        treeClone.Parent = game.Workspace
        treeClone:MoveTo(Vector3.new(math.random(-256, 256), 0, math.random(-256, 256))) -- Generate a random position to move the tree to.
        for _, child in pairs(treeClone:GetChildren()) do
            child.Anchored = true
        end
    end
end
randomTrees()

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