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

How Do I Use Math.Random To Change A Parts Position?

Asked by 1 year ago

Hi so I am a starter scripter and im trying to figure out how can you change the position of a part using math.random so basically I have a spawn and I want the position to be randomized for where the players spawn.

2 answers

Log in to vote
2
Answered by
pwx 1581 Moderation Voter
1 year ago

Well this all depends on how random you wish to have it.

Do you want it randomized on a set of coordinates? If so then you want:

local randomCoordinates = {{5, 2, 5}, {0, 0, 0}, {242, 242, 2525}, {242,4242, 242}}
-- update this table to your desired coordinates

local Part = Instance.new('Part')
local randomCoordinate = randomCoordinates[math.random(1,#randomCoordinates])
local X, Y, Z = randomCoordinate[1], randomCoordinate[2], randomCoordinate[3]
Part.CFrame = CFrame.new(X, Y, Z)
Part.Parent = workspace

Do you want it constantly random regardless of where it spawns?

local Part = Instance.new('Part')
local X, Y, Z = math.random(1,math.huge), math.random(1,math.huge), math.random(1,math.huge)
Part.CFrame = CFrame.new(X, Y, Z)
Part.Parent = workspace
0
Tip: You can use math.randomseed() or Random.new() to make it more accurately random. T3_MasterGamer 2189 — 1y
0
And also, math.random(1, math.huge) will return an error because math.huge is just a concept, not a real number. T3_MasterGamer 2189 — 1y
Ad
Log in to vote
-1
Answered by 1 year ago

Maybe something like this would work??


local Spawn = script.Parent--wherever this part is Spawn.CFrame.X = math.random

This may not work I wasn't able to test it out in studio also this is just a guess to get it to work. Hope this helps!!

Answer this question