Hi, So I’m trying to create a randomized offset for Players Teleportation to a Part from its Center, The Problem is that I don’t know if I need to use CFrame:Inverse or a Function like that to achieve what I want, what should I do?
To achieve a random offset around it, you could do something as simple as this.
local xOffset = math.random(1,5)
--local yOffset = math.random(1,5) Maybe not using Z though.
local zOffset = math.random(1,5)
local part = --PartLocation
local hrp = --The humanoid root part of the player
hrp.CFrame = CFrame.new(part.Position + Vector3.new(xOffset, 0, zOffset))
You could modify their solution and have something like this:
local step = 0.1 --This determines how precise the randomness should be. The smaller the value, the more precise it is (ex: "0.1" could result in "Vector3.new(12.5, 0, 140.8)", "0.01" could result in "Vector3.new(124.04, 0, 34.15)")
local function PickRandomPos(part)
local Pos = part.Position
local Size = part.Size
local Xoff = math.random((-Size.X/2) / step, (Size.X/2) / step) * step
local Yoff = Size.Y/2 --could be replaced with "math.random((-Size.Y/2) / step, (Size.Y/2) / step) * step" if you want a random Y position too.
local Zoff = math.random((-Size.Z/2) / step, (Size.Z/2) / step) * step
local ChosenPosition = Pos + Vector3.new(Xoff, Yoff, Zoff)
return ChosenPosition
end
This will pick a random spot on the part’s surface but can be changed to pick any spot within the part if you applied the “Yoff”