How do i move something based on where a part is? (Read desc)

sorry im not sure how to explain this, but i want to move a part anywhere inside anoher part

so instead of setting the part im trying to make it teleport to a random position within the part

this is what i have right now:

module.Create = function(Part)
	local Drp = script.Drop:Clone()
	Drp.Parent = Part
	Drp.Anchored = false
	Drp.BillboardGui.Frame.BackgroundColor3 = module.Color
	
	--Random POS
	local FinalX = math.random(1, Part.Size.X)
	local FinalZ = math.random(1, Part.Size.Z)
	
	Drp.CFrame = CFrame.new(FinalX, Part.CFrame.Y, FinalZ)
end

but for some reason its only spawning outside the part never inside it

1 Like

should be

Drp.CFrame = Part.CFrame * CFrame.new(FinalX, 0, FinalZ)

so that it spawns relative to it

its still only spawning around the part not in it

Try lowering the bounds by using half the size?

module.Create = function(Part)
    local Drp = script.Drop:Clone()
    Drp.Parent = Part
    Drp.Anchored = false
    Drp.BillboardGui.Frame.BackgroundColor3 = module.Color
    
    local halfSizeX = Part.Size.X / 2
    local halfSizeZ = Part.Size.Z / 2
    local offsetX = math.random(-halfSizeX, halfSizeX)
    local offsetZ = math.random(-halfSizeZ, halfSizeZ)
    
    Drp.CFrame = Part.CFrame * CFrame.new(offsetX, 0, offsetZ)
end

i want the parts to teleport anywhere inside the part not jsut in some places

In that case try:

Drp.CFrame = CFrame.new(Part.Position + Vector3.new(FinalX, 0, FinalZ))

its still only going outside the part

This worked for me in studio, I realised I wasn’t taking into account the size of Drp.

module.Create = function(Part)
    local Drp = script.Drop:Clone()
    Drp.Parent = Part
    Drp.Anchored = false
    Drp.BillboardGui.Frame.BackgroundColor3 = module.Color
    
    local minX = Part.Position.X - Part.Size.X/2 + Drp.Size.X/2
    local maxX = Part.Position.X + Part.Size.X/2 - Drp.Size.X/2
    local minZ = Part.Position.Z - Part.Size.Z/2 + Drp.Size.Z/2
    local maxZ = Part.Position.Z + Part.Size.Z/2 - Drp.Size.Z/2
    
    local offsetX = math.random(minX, maxX)
    local offsetZ = math.random(minZ, maxZ)
    
    Drp.CFrame = CFrame.new(
        offsetX,
        Part.Position.Y + Drp.Size.Y/2, 
        offsetZ
    )
end

If the range is too limited and is not spawning in some places of the Part that you want it to then you can change the range of it.

function moveToRandomPositionWithinBounds(targetPart, movingPart)
    -- Get the size and CFrame of the target part
    local targetSize = targetPart.Size
    local targetCFrame = targetPart.CFrame
    
    -- Calculate the local min and max bounds of the target part
    local minX = -targetSize.X / 2
    local maxX = targetSize.X / 2
    local minY = -targetSize.Y / 2
    local maxY = targetSize.Y / 2
    local minZ = -targetSize.Z / 2
    local maxZ = targetSize.Z / 2
    
    -- Generate a random local position within the bounds
    local randomX = math.random() * (maxX - minX) + minX
    local randomY = math.random() * (maxY - minY) + minY
    local randomZ = math.random() * (maxZ - minZ) + minZ
    local randomLocalPosition = Vector3.new(randomX, randomY, randomZ)
    
    -- Convert the local position to a world position
    local randomWorldPosition = targetCFrame:PointToWorldSpace(randomLocalPosition)
    
    -- Move the moving part to the random world position
    movingPart.CFrame = CFrame.new(randomWorldPosition)
end

This’ll move movingPart into a random position within targetPart. It doesn’t account for movingPart’s size, so it’s possible that it gets moved onto the edge (which means it’d extend outside the part).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.