Not placing tower but there is no errors

Hello fellow Developers! I am doing a game jam that is due tomorrow and I have a problem where the tower won’t place even though there are no errors or inf yields in the output. Here is my code:

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

local gold = Players.LocalPlayer:WaitForChild("Gold")
local events = ReplicatedStorage:WaitForChild("Events")
local functions = ReplicatedStorage:WaitForChild("Functions")
local requestTowerFunction = functions:WaitForChild("RequestTower")
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 rotation = 0
local placedTowers = 0
local maxTowers = 20

local function UpdateGold()
	gui.Gold.Text = gold.Value
end
UpdateGold()
gold.Changed:Connect(UpdateGold)

local function MouseRaycast(exclude)
	local mousePosition = UserInputService:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
	local raycastParams = RaycastParams.new()
	
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = exclude

	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)
	
	return raycastResult
end

local function RemovePlaceholderTower()
	if towerToSpawn then
		towerToSpawn:Destroy()
		towerToSpawn = nil
		rotation = 0
	end
end

local function AddPlaceholderTower(name)
	
	local towerExists = towers:FindFirstChild(name)
	if towerExists then
		RemovePlaceholderTower()
		towerToSpawn = towerExists:Clone()
		towerToSpawn.Parent = workspace
		
		for i, object in ipairs(towerToSpawn:GetDescendants()) do
			if object:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(object, "Tower")
				object.Material = Enum.Material.ForceField
				object.Transparency = 0.3
			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.Towers.Title.Text = "Towers: " .. placedTowers .. "/" .. maxTowers
for i, tower in pairs(towers:GetChildren()) do
	local button = gui.Towers.Template:Clone()
	local config = tower:WaitForChild("Config")
	button.Name = tower.Name
	button.Image = config.Image.Texture
	button.Visible = true
	button.LayoutOrder = config.Price.Value
	button.Price.Text = config.Price.Value
	
	button.Parent = gui.Towers
	
	button.Activated:Connect(function()
		local allowedToSpawn = requestTowerFunction:InvokeServer(tower.Name)
		if allowedToSpawn then
			AddPlaceholderTower(tower.Name)
		end
	end)
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)
				placedTowers += 1
				gui.Towers.Title.Text = "Towers: " .. placedTowers .. "/" .. maxTowers
				RemovePlaceholderTower()
			end
		elseif input.KeyCode == Enum.KeyCode.R then
			rotation += 90
		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))
				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) * CFrame.Angles(0, math.rad(rotation), 0)
			towerToSpawn:SetPrimaryPartCFrame(cframe)
		end
	end
end)
5 Likes

Where do you think the error is happening? Making Module Scripts spares you so much work to find out the bug tho.

2 Likes

Well I think it is happening in the if result and result.Instance.Parent.Name == "TowerArea" then

3 Likes

I have modules for the mob and tower script

2 Likes

And all the tower module script does is damage the enemy.

4 Likes

Have you ever tried to debbug the script? Like you know, the red ball on the script.

4 Likes

How do you debug the local script?

4 Likes

I just said local script to have 30 letters so I can send the message.

4 Likes

You do not know what does debug is? Debugging | Documentation - Roblox Creator Hub here is some information about it.

Debugging is another way to you find the error that actually do not show on the command bar, I.E it’s another way to find an error.

4 Likes

I know what it is I just never use it

4 Likes

Yea sure, you can debug it by clicking on one of the lines on there. Try to make that on the line “If result…”

3 Likes

Ok! Let me see if it the script works.

4 Likes

When it starts debugging, you can hover the mouse into the text to see what happens! Seeing attributes, values and returns!

3 Likes

The script has no errors and the debugger didn’t say or do anything.

3 Likes

try debugging by printing:
print(“Result:”, result)
if result and result.Instance.Parent.Name == “TowerArea” then
print(“Inside TowerArea”)
– …
end

5 Likes

Did you passed the mouse into it? Did you check if the debug is really on there?

2 Likes

Yea that is another way too. (the limit blah)

2 Likes

haha its the way i debug if i cant debug through errors i just add print statements everywhere until i understand what is messing with my variables or whatever im doing LOL

I mean tbf thats prolly what alot of people do

2 Likes

The script didn’t print anything.

2 Likes

Wait, is this a local script or a server script? Also, are you using remote events/function events? I didn’t see any there.

1 Like