Not placing tower but there is no errors

That can means that the script is not reaching the print though.

1 Like

It’s a local script. Is that the problem?

1 Like

try this:
local result = MouseRaycast({towerToSpawn})
print(“1”)
if result and result.Instance then
print(“2”)
if result.Instance.Parent.Name == “TowerArea” then
print(“3”)
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

there could be an issue where the if statement stops it because maybe its done wrong

1 Like

Where is the local script placed?

1 Like

Also only in the tower script I use a remote function:

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 animateTowerEvent = events:WaitForChild("AnimateTower")
local functions = ReplicatedStorage:WaitForChild("Functions")
local requestTowerFunction = functions:WaitForChild("RequestTower")

local maxTowers = 10
local tower = {}

function FindNearestTarget(newTower, range)
	local nearestTarget = nil

	for i, target in ipairs(workspace.Mobs:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		if distance < range then
			nearestTarget = target
			range = distance
		end
	end

	return nearestTarget
end

function tower.Attack(newTower, player)
	local config = newTower.Config
	local target = FindNearestTarget(newTower, config.Range.Value)
	
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		
		local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
		newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
		
		animateTowerEvent:FireAllClients(newTower, "Attack")
		
		target.Humanoid:TakeDamage(config.Damage.Value)
		
		if target.Humanoid.Health <= 0 then
			player.Gold.Value += target.Humanoid.MaxHealth
		end
		
		task.wait(config.Cooldown.Value)
	end

	task.wait(0.1)
	
	tower.Attack(newTower, player)
end

function tower.Spawn(player, name, cframe)
	local allowedToSpawn = tower.CheckSpawn(player, name)
	
	if allowedToSpawn then

		local newTower = ReplicatedStorage.Towers[name]:Clone()
		newTower.HumanoidRootPart.CFrame = cframe
		newTower.Parent = workspace.Towers
		newTower.HumanoidRootPart:SetNetworkOwner(nil)
		
		local bodyGyro = Instance.new("BodyGyro")
		bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
		bodyGyro.D = 0
		bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
		bodyGyro.Parent = newTower.HumanoidRootPart

		for i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(object, "Tower")
			end
		end
		
		player.Gold.Value -= newTower.Config.Price.Value
		player.PlacedTowers.Value += 1
		
		coroutine.wrap(tower.Attack)(newTower, player)
	else
		warn("Requested tower does not exist:", name)
	end
end

spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)


function tower.CheckSpawn(player, name)
	local towerExists = ReplicatedStorage.Towers:FindFirstChild(name)

	if towerExists then
		if towerExists.Config.Price.Value <= player.Gold.Value then
			if player.PlacedTowers.Value < maxTowers then
				return true
			else
				warn("Player has reached max limit")
			end
		else
			warn("Player cannot afford")
		end
	else
		warn("That tower does not exist")
	end
	
	return false
end
requestTowerFunction.OnServerInvoke = tower.CheckSpawn

return tower

1 Like

Where is the module script placed?

Inside of a wavescript that starts the wave. Also the wavescript is a regular script.

Where is the wavescript? (blah blah blah) and the local script

The script is in ServerScriptService

I found the problem, LocalScript cannot access the ModuleScript through the ServerScriptService, Players do not have the access to do that, try changing the ModuleScript to the ReplicatedFirst or ReplicatedStorage.

2 Likes

The placeholder tower is not spawning

Hum that is strange, can you take a screenshot of the ModuleScriptPlacement?

1 Like

Change that to Replicated Storage

I did and it is still not working.

Try printing on the Start of the script something like: “I am here”

I did and it is still not printing.

Dude why is that happening lol It seems the Local Script is not running!

Have you never thinked the problem would be on the remote function?

No. I never thought it would be a remote function.