So pretty much when placing in my tower defense game the tower gets lifted up for some reason. I have two scripts one as a local script which is this
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 canPlace = false
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 RemovePlaceHolderTower()
if towerToSpawn then
towerToSpawn:Destroy()
towerToSpawn = nil
end
end
local function AddPlaceHolderTower(name)
local towerExists = towers:FindFirstChild(name)
if towerExists then
RemovePlaceHolderTower()
towerToSpawn = towerExists:Clone()
towerToSpawn.Parent = workspace.Towers
for i, object in ipairs(towerToSpawn:GetDescendants()) do
if object:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(object, "Tower")
object.Material = Enum.Material.ForceField
end
end
end
end
local function ColorPlaceHoldertower(color)
for i, object in ipairs(towerToSpawn:GetDescendants()) do
if object:IsA("BasePart") then
object.Color = color
end
end
end
Gui.Spawn.Activated:Connect(function()
AddPlaceHolderTower("Rifleman")
end)
UserInputService.InputBegan:Connect(function(input, processed)
if processed then
return
end
if towerToSpawn then
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if canPlace then
spawnTowerEvent:FireServer(towerToSpawn.Name, towerToSpawn.PrimaryPart.CFrame)
RemovePlaceHolderTower()
end
end
end
end)
RunService.RenderStepped:Connect(function()
if towerToSpawn then
local result = MouseRayCast({towerToSpawn})
if result and result.Instance then
if result.Instance.Parent.Name == "TowerArea" then
canPlace = true
ColorPlaceHoldertower(Color3.new(0,1,0))
else
canPlace = false
ColorPlaceHoldertower(Color3.new(1,0,0))
end
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)
Then this is the script where problem most likely lies. Its a module script:
local PhysicsService = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local tower = {}
function tower.Spawn(player, name, cframe)
local towerExists = ReplicatedStorage.Towers:FindFirstChild(name)
if towerExists then
local newTower = towerExists:Clone()
newTower.HumanoidRootPart.CFrame = cframe
newTower.Parent = workspace.Towers
newTower.HumanoidRootPart:SetNetworkOwner(nil)
for i, object in ipairs(newTower:GetDescendants()) do
if object:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(object, "Tower")
end
end
else
warn("Requested tower does not exist", name)
end
end
spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)
return tower
If anybody can tell me where I went wrong I’d greatly appreciate it!