Tower placing system levitating towers upon placement

I have a tower placing script. Basically whenever I place a tower it is levitated above the ground around 2 studs.
image

here is the script which i think causes it.

local PhysicsService = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local events = ReplicatedStorage:WaitForChild("Events")
local towers = ReplicatedStorage:WaitForChild("Towers")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local camera = workspace.CurrentCamera
local gui = script.Parent

local towerToSpawn = nil

local function MouseRaycast(blacklist)
	local mousePosition = UserInputService:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
	local raycastParams = RaycastParams.new()
	
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = blacklist
	
	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)
	
	
	return raycastResult
end

local function AddPlaceholderTower(name)
	
	local towerExists = towers:FindFirstChild(name)
	if towerExists then
		towerToSpawn = towerExists:Clone()
		towerToSpawn.Parent = workspace.Towers
		
		for i, object in ipairs(towerToSpawn:GetDescendants()) do
			if object:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(object, "Towers")
			end
		end
	end
end

gui["main-game"].Frame.Frame.Spawn.Activated:Connect(function()
	AddPlaceholderTower("Pistol")
end)

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		spawnTowerEvent:FireServer(towerToSpawn.Name, towerToSpawn.PrimaryPart.CFrame)
	end
end)

RunService.RenderStepped:Connect(function()
	if towerToSpawn then
		local result =  MouseRaycast({towerToSpawn})
		if result and result.Instance then
			local x = result.Position.X
			local y = result.Position.Y + towerToSpawn.Humanoid.HipHeight + (towerToSpawn.PrimaryPart.Size.Y / 2)
			local z = result.Position.Z

			local cFrame = CFrame.new(x,y,z)
			towerToSpawn:SetPrimaryPartCFrame(cFrame)
		end
	end
end)
also this error appears:
![image|560x500](upload://mnR761Wm3FrlcseC0rdM5Nvy0wo.png)


1 Like

aswell as that this error appears

1 Like

I don’t have an exact solution for you but I do notice you’re using SetPrimaryPartCFrame, which is deprecated. Use PivotTo instead. It’s simpler and it allows you to control the pivot point of your models, which may help you with placing your models properly

Humanoid is not a valid member of Model "Pistol"
You’re attempting to try to find an instance Humanoid that doesn’t exist in the pistol Model. When you reference the Humanoid, if you know it’s going to exist, use WaitForChild so you don’t miss it

1 Like

also keep in mind that the rig im using is r6

1 Like

image

1 Like