What's wrong with this script?

I’m trying to make a tower placement system for my game. When I press a button, it clones it into the workspace, but it doesn’t follow my mouse or place when I click. Can someone tell me what’s wrong with this?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceTower = ReplicatedStorage:WaitForChild("PlaceTower")
local Tower = ReplicatedStorage.Towers.Minigunner
local Towers = ReplicatedStorage:WaitForChild("Towers")

local UIS = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local Character = player.Character or player.Character:Wait()

local Mouse = player:GetMouse()
local placingTower = false
local TowerFrame = script.Parent.TowerFrame

for _, towerButton in pairs(TowerFrame:GetChildren()) do
	if towerButton:IsA("TextButton") then
		towerButton.MouseButton1Up:Connect(function()

			TowerFrame.Visible = false

			local goodToPlace = false
			local placedTower

			if placingTower == false then
				placingTower = true

				local clientTower = Towers:FindFirstChild(towerButton.Name):Clone()
				clientTower.Hitbox.SelectionBox.Color3 = Color3.new(0.154482, 0.739025, 0.102632)
				clientTower.Hitbox.BrickColor = BrickColor.new("Forest green")
				clientTower.Parent = game.Workspace.Towers

Mouse.TargetFilter = Character
game:GetService("RunService").RenderStepped:Connect(function()
	if Mouse.Target ~= nil then
		Tower.HumanoidRootPart.CFrame = CFrame.new(Mouse.Hit.Position) * CFrame.new(0, 8.836, 0) -- Offset
	end
end)
			end
		end)
	end
end
1 Like

Well one issue is

You’re setting Tower, when you should be setting clientTower I believe.

You might also want to think about how to stop the dragging :slight_smile:

All of this tower placement is all happening on the client, the general consensus is to never trust the client for such tasks. This isn’t just for security sake but also for general useabilitiy. No other user, apart from you, can actually see that tower being placed or moved around (if you’d want players to see the movement). Such things like this aren’t replicated, as it’s not intended or ROBLOX’s responsibility to replicate actions like this.

Tackling the issue properly
Now as for the issue, you can use print statements to see where the script doesn’t execute. People underestimate the power of something as simple as print statements. They’re really useful when it comes to debugging stuff.

So it did that, but now the tower bounces back and forth towards the camera. The only way to stop it is by setting the Y offset to something high so it doesn’t touch the ground, which looks weird.

You need to set Mouse.TargetFilter to the model.

1 Like

try this

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceTower = ReplicatedStorage:WaitForChild("PlaceTower")
local Tower = ReplicatedStorage.Towers.Minigunner
local Towers = ReplicatedStorage:WaitForChild("Towers")

local UIS = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local Character = player.Character or player.Character:Wait()

local Mouse = player:GetMouse()
local placingTower = false
local TowerFrame = script.Parent.TowerFrame

for _, towerButton in pairs(TowerFrame:GetChildren()) do
	if towerButton:IsA("TextButton") then
		towerButton.MouseButton1Up:Connect(function()

			TowerFrame.Visible = false

			local goodToPlace = false
			local placedTower

			if placingTower == false then
				placingTower = true

				local clientTower = Towers:FindFirstChild(towerButton.Name):Clone()
				clientTower.Hitbox.SelectionBox.Color3 = Color3.new(0.154482, 0.739025, 0.102632)
				clientTower.Hitbox.BrickColor = BrickColor.new("Forest green")
				clientTower.Parent = game.Workspace.Towers

				Mouse.TargetFilter = clientTower
				game:GetService("RunService").RenderStepped:Connect(function()
					if Mouse.Target ~= nil then
						clientTower.PrimaryPart.CFrame = CFrame.new(Mouse.Hit.Position) * CFrame.new(0, clientTower.Hitbox.Size.Y/2, 0) -- Offset
					end
				end)
			end
		end)
	end
end