How can I prevent models from falling through the floor when they spawn?

I tried to create a spawn from raycast, but the problem is still unsolved

Spawn Code:

`

local zone = game.Workspace.Spawns[math.random(1,2)]
local model = game.ReplicatedStorage.Brainrots:FindFirstChild(brainrot):Clone()
local infoTween = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
model.Parent = workspace
local x = zone.Position.X
local xS = zone.Size.X / 2
local y = zone.Position.Y + (zone.Size.Y / 2) + model.PrimaryPart.Size.Y
local z = zone.Position.Z
local zS = zone.Size.Z / 2
local EndPos = CFrame.new(math.random(math.min(x - xS), math.max(xS + x)), y, math.random(math.min(z - zS), math.max(zS + z)))
local StartPos = EndPos + Vector3.new(0,100,0)
local origin = StartPos.Position
local direction = StartPos.Position - Vector3.new(0,1000,0)
local ao = nil
local ray = workspace:Raycast(origin, direction)

if ray then
	print(ray.Position)
	ao = CFrame.new(ray.Position.X,ray.Position.Y,ray.Position.Z)
end

model.PrimaryPart.Anchored = true
model:PivotTo(StartPos)
local cV = Instance.new("CFrameValue")
cV.Value = model:GetPivot()
local t = game.TweenService:Create(cV, infoTween, {Value = ao})
t:Play()

cV.Changed:Connect(function()
	model:PivotTo(cV.Value)
end)

end

`

you are using model:PivotTo() which puts the pivot of the model at the specified position. (which will position the whole model)


you want to position the pivot around the legs.

you can also just add model:GetExtentsSize().Y/2 to the target position

Hey, you should simply just add to the ray.Position.Y the Y size of the brainrot/2 as it is already halfway through the floor

Updated Bit:

ao = CFrame.new(ray.Position.X,ray.Position.Y+model.PrimaryPart.Size.Y/2,ray.Position.Z)
local zone = game.Workspace.Spawns[math.random(1,2)]
local model = game.ReplicatedStorage.Brainrots:FindFirstChild(brainrot):Clone()
local infoTween = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
model.Parent = workspace
local x = zone.Position.X
local xS = zone.Size.X / 2
local y = zone.Position.Y + (zone.Size.Y / 2) + model.PrimaryPart.Size.Y
local z = zone.Position.Z
local zS = zone.Size.Z / 2
local EndPos = CFrame.new(math.random(math.min(x - xS), math.max(xS + x)), y, math.random(math.min(z - zS), math.max(zS + z)))
local StartPos = EndPos + Vector3.new(0,100,0)
local origin = StartPos.Position
local direction = StartPos.Position - Vector3.new(0,1000,0)
local ao = nil
local ray = workspace:Raycast(origin, direction)

if ray then
	print(ray.Position)
	ao = CFrame.new(ray.Position.X,ray.Position.Y+model.PrimaryPart.Size.Y/2,ray.Position.Z)
end

model.PrimaryPart.Anchored = true
model:PivotTo(StartPos)
local cV = Instance.new("CFrameValue")
cV.Value = model:GetPivot()
local t = game.TweenService:Create(cV, infoTween, {Value = ao})
t:Play()

cV.Changed:Connect(function()
	model:PivotTo(cV.Value)
end)

Thanks! Thats work but i should change HumanoidRootPart Position

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