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

How To Spawn A Model Randomly Without Lining Them Up?

Asked by 4 years ago
Edited 4 years ago

I learnt to code random positions from a Youtube video. Then, I applied it to make my model fall from random positions. Disappointingly, the grapes only lined up. DM on discord Starlight#7272 if you want more details. This is my code:

local Grape = game.ReplicatedStorage:WaitForChild('Grape')
local grapeHeight = 100
local MinX = -100
local MaxX = 100
local MaxZ= 100
local MinZ = -100


math.randomseed(tick())
local function grape()
    local GrapeCopy = Grape:Clone()
    local x = math.random(MinX,MaxX)
    local z math.random(MinZ,MaxZ)
GrapeCopy.Parent = workspace
GrapeCopy.CFrame = CFrame.new(x,grapeHeight,z)
game:GetService('Debris'):AddItem(GrapeCopy,math.random(10,15))


while true do
    grape()
    wait(1)
end

Could anyone explain why it is not working?

1 answer

Log in to vote
0
Answered by 4 years ago

Not sure why that one didn't work, but I re-wrote that script into a shorter version and it works perfectly fine.

Here is what I did.

local minX = -100
local maxX = 100
local norY = 100
local minZ = -100
local maxZ = 100

while wait(1) do
    math.randomseed(tick())
    local grape = game:GetService("ReplicatedStorage"):WaitForChild("Grape"):Clone()
    grape.Parent = game:GetService("Workspace")
    grape.CFrame = CFrame.new(math.random(minX,maxX),norY,math.random(minZ,maxZ))
    game:GetService("Debris"):AddItem(grape,math.random(10,15))
end

I recommend putting math.randomseed(tick()) inside of the function so it randomizes every time.

I just compiled it down into less code is about it, and it works. It ranges from -100 to 100, and does not go into a straight line.

Hope this helped!

0
I’m gonna check this when I’m home. Thank you. Also, it may be because I didn’t put random.seed(tick()) in the function, which is why I didn’t work. Nozo_rapsocooz 16 — 4y
0
I'm not sure, but I'm glad to be of help! killerbrenden 1537 — 4y
0
Oh I forgot the equal mark on line 13. Thanks for shortening the script. Nozo_rapsocooz 16 — 4y
0
No problem! killerbrenden 1537 — 4y
Ad

Answer this question