Tower Defense Unit Limit

Hiii Im currently working on a tower defense game and I was wondering how can I add a limit on how much of a certain unit I can place. As you can see, the limit is 4. What is the best way that I can implement this

2 Likes

Maybe like adding +1 and then make if there is finally 4 then not be able to place anymore.
I am not a scripter but I hopr you understand.

2 Likes

add a ( numberValue/number attribute ) when the player first join and when the player send a remote event check if his numberValue is < the tower Placement limit and if it is then increment the numberValue by 1

2 Likes

function UnitSpawnModule.Spawn(player, name, cframe)
local AllowedToSpawn = UnitSpawnModule.CheckSpawn(player, name)

if AllowedToSpawn then
	print("Spawning "..name)
	
	local newUnit = ReplicatedStorage.Units[name]:Clone()
	newUnit:WaitForChild("HumanoidRootPart").CFrame = cframe
	newUnit.Parent = workspace.Units
	
	
	local UnitNumberExists = player:FindFirstChild(name)

	if UnitNumberExists then
		local UnitNumber = player:FindFirstChild(name)
		UnitNumber.Value += 1
		
	else
		local UnitNumber = Instance.new("IntValue")
		UnitNumber.Name = name
		UnitNumber.Parent = player
		UnitNumber.Value = 0
		UnitNumber.Value +=1
		UnitAddEvent:FireClient(player)
	end
	
	
		newUnit.HumanoidRootPart:SetNetworkOwner(nil)
		--local humPart = newUnit:WaitForChild("HumanoidRootPart")


		local bodyGyro = Instance.new("BodyGyro")
		bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
		bodyGyro.D = 0
		bodyGyro.CFrame = newUnit.HumanoidRootPart.CFrame
		bodyGyro.Parent = newUnit.HumanoidRootPart

		player.Money.Value -= newUnit.Config.Cost.Value
		
	   
		


		coroutine.wrap(UnitSpawnModule.Attack)(newUnit, player)
	

	
else
	warn("Requested Unit does not exist: "..name)
end

end

This is the code I made in my module script: (The module will look for an int value with the same name as the Unit I will spawn. if it doesn’t find one, It will create an Int value inside the player and will add 1 to the value. Im trying to figure out now how to implement a limit based on a int value inside the Unit itself. Here is the example with Finn:

function UnitSpawnModule.Spawn(player, name, cframe)
local AllowedToSpawn = UnitSpawnModule.CheckSpawn(player, name)
local Count = 0
for i,v in pairs(workspace.Units:GetChildren()) do
    if v.Name == name then
        Count+=1
    end
end
if ReplicatedStorage.Units[name].Config.Limit then
    if Count >= ReplicatedStorage.Units[name].Config.Limit.Value then
        AllowedToSpawn = false -- Limit reached
    end
else
    warn(name.."has no spawn limit")
end
if AllowedToSpawn then
	print("Spawning "..name)
	
	local newUnit = ReplicatedStorage.Units[name]:Clone()
	newUnit:WaitForChild("HumanoidRootPart").CFrame = cframe
	newUnit.Parent = workspace.Units
	
	
	local UnitNumberExists = player:FindFirstChild(name)

	if UnitNumberExists then
		local UnitNumber = player:FindFirstChild(name)
		UnitNumber.Value += 1
		
	else
		local UnitNumber = Instance.new("IntValue")
		UnitNumber.Name = name
		UnitNumber.Parent = player
		UnitNumber.Value = 0
		UnitNumber.Value +=1
		UnitAddEvent:FireClient(player)
	end
	
	
		newUnit.HumanoidRootPart:SetNetworkOwner(nil)
		--local humPart = newUnit:WaitForChild("HumanoidRootPart")


		local bodyGyro = Instance.new("BodyGyro")
		bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
		bodyGyro.D = 0
		bodyGyro.CFrame = newUnit.HumanoidRootPart.CFrame
		bodyGyro.Parent = newUnit.HumanoidRootPart

		player.Money.Value -= newUnit.Config.Cost.Value
	coroutine.wrap(UnitSpawnModule.Attack)(newUnit, player)
else
	warn("Requested Unit does not exist: "..name)
end
1 Like

Wow… Thanks I will try that out and give you feedbackk!!

Yowww man this actually worked! Thank youu so muchhh!! Im new here, how do I mark your reply as the solution???

I dont actually know lol, is there maybe the 3 dots or something?

yess but it only shows me the option to flag, share and reply to your post

there we go, The post needs to be “Help and Feedback Code Review” to be able to mark a solution… Thanks so much for your helpp and everyone who replied

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.