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

how to make a random BodyPosition value?

Asked by 2 years ago

I tried searching it on the web and was trying to makxe my script but it doesn't work. what I want to do is have the script make the value (pos,32,zpos) where xpos and ypos are random numbers between 92 and -92.

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
2 years ago

little function i whipped up .... Might have some bugs in it as i've havent tested it fully but heres the function and how to call it for your random Vector3 Pos:

local function RandomPos(XMin,XMax,YMin,YMax,ZMin,ZMax)
    local XRMin = math.random() * XMin
    local XRMax = math.random() * XMax
    local YRMin = math.random() * YMin
    local YRMax = math.random() * YMax
    local ZRMin = math.random() * ZMin
    local ZRMax = math.random() * ZMax
    local function Check(Min,Max) 
        if(Min > Max)then 
            return math.random(Max,Min)
        end 
        return math.random(Min,Max) 
    end
    local NewX = math.random()*Check(XRMin,XRMax)
    local NewY = math.random()*Check(YRMin,YRMax)
    local NewZ = math.random()*Check(ZRMin,ZRMax)
    return Vector3.new(NewX,NewY,NewZ)
end

--// How to call this function 
for i=1, 100 do
    local Pos = RandomPos(-92,92,0,0,-92,92)
    --// this bit is to "Show" the function in operation in the workspace with a part at the new random position!
    local Part = (function()
        local P = Instance.new("Part",workspace)
        P.Anchored = true
        P.Position = Pos -- Assign this new part its position generated by the above RandomPos Funct
    end)()

    wait()
end

Hope this helps! :)

Ad

Answer this question