Hello, recently I’ve been making a TD game and I’m having a problem where my keeps moving up when I try to place it.
Video:
Server Code:
function self.Spawn(player, name, cframe)
local Exist = ServerStorage.Humanoids.Towers:FindFirstChild(name):FindFirstChild(name.."_Level_1")
local TowerData = _G.GetModule("TowerData")[name].Level_1
if Exist and _G.GetProfile(player).Data.Cash > TowerData.Cost then
local NewTower = Exist:Clone()
NewTower.HumanoidRootPart.CFrame = cframe
_G.SetGroupModel(NewTower, "Tower")
NewTower.Parent = workspace.Towers
local bodyGyro = Instance.new("BodyGyro")
bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
bodyGyro.D = 0
bodyGyro.CFrame = NewTower:WaitForChild("HumanoidRootPart").CFrame
bodyGyro.Parent = NewTower.HumanoidRootPart
task.spawn(self.Attack(NewTower, TowerData))
else
_G.Error("Tower "..name.." does not exist!")
end
end
Client Side Code:
return function()
local UIS = game:GetService("UserInputService")
local Tower
local canPlace = false
local rotation = 0
local RS = game:GetService("ReplicatedStorage")
local Remotes = RS.Events
local PlaceRemote = Remotes.PlaceTower
local TOWERS_FOLDER = RS:WaitForChild("TOWERS")
function _G.DeletePlaceholder()
if Tower then
Tower:Destroy()
Tower = nil
end
rotation = 0
end
function _G.ShowPlaceholder(Name)
local towerExist = TOWERS_FOLDER:FindFirstChild(Name)
if towerExist then
_G.DeletePlaceholder()
Tower = towerExist:Clone()
Tower.Parent = workspace.MouseFilter
_G.SetGroupModel(Tower, "Tower")
end
end
local function ColorPlaceholder(color)
for _, part in pairs(Tower:GetDescendants()) do
if part:IsA("BasePart") then
part.Color = color
part.CanQuery = false
end
end
end
local UI = _G.Player:WaitForChild("PlayerGui"):WaitForChild("TowerPlacement"):WaitForChild("PlacementFrame")
UIS.InputBegan:Connect(function(input, processed)
if processed then
return
end
if Tower then
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if canPlace then
PlaceRemote:FireServer(Tower.Name, Tower.PrimaryPart.CFrame)
_G.DeletePlaceholder()
end
elseif input.KeyCode == Enum.KeyCode.R then
rotation += 90
end
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
if Tower then
local result = _G.MouseRay({Tower})
if result and result.Instance then
if result.Instance.Parent.Name == "TowerArea" then
canPlace = true
ColorPlaceholder(Color3.new(0, 1, 0))
else
ColorPlaceholder(Color3.new(1, 0, 0))
canPlace = false
end
local x = result.Position.X
local y = result.Position.Y + Tower.Humanoid.HipHeight + (Tower.PrimaryPart.Size.Y / 2)
local z = result.Position.Z
local cframe = CFrame.new(x, y, z) * CFrame.Angles(0, math.rad(rotation), 0)
Tower:SetPrimaryPartCFrame(cframe)
end
end
end)
end
Thank you!