Issues with tower placement system

hi guys, so the problem im facing is that when i try to place the tower (click the mouse) the tower does not clone. it was working before but stopped working after i added in a check to see where the tower is being placed. it tried using overlapParams to see what parts are being touched by tthe tower but that did not work. heres the code

local buttons = {}
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local isPlacing = false
local canPlace = false

local UIS = game:GetService("UserInputService")

function buttons.ClickButton(button)
	local buttonTower
	local newTower
	local currentPosition 
	local bind
	local unitRay
	local currentPart

	button.MouseButton1Click:Connect(function()
		buttonTower = button:WaitForChild("Tower").Value
	    newTower = buttonTower:Clone()
		isPlacing = true

	    newTower.Parent = workspace

		local runService = game:GetService("RunService")
		bind = runService.RenderStepped:Connect(function()
			if isPlacing == true then
				local mouseLocation = UIS:GetMouseLocation()
				
				newTower:WaitForChild("Hitbox").Touched:Connect(function(hit)
					currentPart = hit.Name
				end)

				local hrp = newTower:WaitForChild("HumanoidRootPart")
				local TSS = game:GetService("TweenService")
				local inf = TweenInfo.new(.2)

				local camera = workspace.CurrentCamera

				local params = RaycastParams.new()
				params.FilterDescendantsInstances = {character, hrp, hrp.Parent}
				params.FilterType = Enum.RaycastFilterType.Blacklist 

				local function rayResult(x, y)
					unitRay = camera:ScreenPointToRay(x, y)
					return workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, params)

				end

				local result = rayResult(UIS:GetMouseLocation().X, UIS:GetMouseLocation().Y)
				local hit = result.Instance 

				local x;
				local y;
				local z;

				if hit then
					x = result.Position.X
					y = result.Position.Y
					z = result.Position.Z			

				end

				for _, v in ipairs(newTower:GetDescendants()) do 
					if v:IsA("Part") then
						v.Transparency = 0.5
						v.BrickColor = BrickColor.new("Lime Green")
					end
				end


				currentPosition = CFrame.new(x, y, z)
				local enf = {["CFrame"] = CFrame.new(x, 0, z) * CFrame.new(0, 3, 0)}
				TSS:Create(hrp, inf, enf):Play()
			else
				return nil
			end
		end)
	end)

	UIS.InputBegan:Connect(function(i, gpx)
		if not gpx and i.UserInputType == Enum.UserInputType.MouseButton1 and isPlacing == true then
			if currentPart then
				if currentPart.Name == "CanPlace" then
					game.ReplicatedStorage.PLACE:FireServer(currentPosition, buttonTower)
					newTower:Destroy()
					
					isPlacing = false
				elseif currentPart.Name == "NoPlace" then
					print("cant place lol")	
				end
			end
		end
	end)
end

return buttons

the above code is in a module and is being called in a client script
any help or advice is greatly appreciated
thanks

You’re cloning a value? Not sure if that’s part of the change that you did for the check.

“Tower” is an objectValue in the TextButton
im cloning the Value of tower, which is a towerModel in replicated storage

It stopped working after you added which part of code? Because from what I’m looking I can’t really see anything wrong…

well, Currentpart gets set to hit.name, which is the name of whatever hit the hitbox. So unless the hit.name is == “CanPlace”, it wont fire.

1 Like

i get what ur saying but what do i need to change in my code to fix the bug

oh wait, nvm i fixed it
just needed to remove the .Name after currentPart over here

if currentPart.Name == "CanPlace" then
					game.ReplicatedStorage.PLACE:FireServer(currentPosition, buttonTower)
					newTower:Destroy()
					
					isPlacing = false
and in the elseif statement

thanks a lot for helping me comrade

no problem, glad we figured it out man :slight_smile:

1 Like