Model is not a valid member of folder

I’ve been trying this for a few hours to get this code to works but my understanding of script logic is not high enough

What i’m been trying to do is upon selecing a tower icon and placing it around the map, it spawns the tower on top of removing cash from the player amount.

12:59:51.533  Guide is not a valid member of Folder "ReplicatedStorage.TowersFolder"  -  Server - TowerScript:61
  12:59:51.533  Stack Begin  -  Studio
  12:59:51.533  Script 'ServerScriptService.MainScript.TowerScript', Line 61 - function SpawnTower  -  Studio - TowerScript:61
  12:59:51.533  Stack End  -  Studio

Here’s the entire code, both from the module script

local PhysicsService = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local events = ReplicatedStorage:WaitForChild("EventsFolder")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local animateTowerEvent = events:WaitForChild("AnimateTower")

local functions = ReplicatedStorage:WaitForChild("FunctionsFolder")
local requestTowerFunction = functions:WaitForChild("RequestTower")

local tower = {}


function FindNearestTarget(newTower, range)
	local nearestTarget = nil
	
	for i, target in ipairs(workspace.CurrentWave: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.AttackEnemy(newTower, player)
	local config = newTower.TowerConfig
	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, "AttackAnim")
		
		target.Humanoid:TakeDamage(config.Damage.Value)
		
		if target.Humanoid.Health > 0 then
			player.Cash.Value += config.Damage.Value
		elseif target.Humanoid.Health <= 0 then
			player.Cash.Value += config.Damage.Value + target.Humanoid.Health
		end
		
		task.wait(config.Firerate.Value)
	end
	
	task.wait(0.1)
	
	tower.AttackEnemy(newTower, player)
end


function tower.SpawnTower(player, name, cframe)
	local allowedToSpawn = tower.CheckSpawn(player, name)
	
	if allowedToSpawn then
		local newTower = ReplicatedStorage.TowersFolder[name]:Clone()
		
		newTower.Parent = workspace.CurrentPlacedTowers
		newTower.HumanoidRootPart:SetNetworkOwner(nil)
		newTower.HumanoidRootPart.CFrame = cframe
		
		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.Cash.Value -= newTower.TowerConfig.Price.Value
		if newTower.TowerConfig.IsAHero.Value == true then
			player.PlacedHeroTower = true
		end
			
		coroutine.wrap(tower.AttackEnemy)(newTower, player)
	else
		warn("Requested tower does not exist:", name)
	end
end

spawnTowerEvent.OnServerEvent:Connect(tower.SpawnTower)


function tower.CheckSpawn(player, name)
	local towerExists = ReplicatedStorage.TowersFolder:FindFirstChild(name)
	local heroTowerExist = towerExists.HeroesTowers:FindFirstChild(name)

	if towerExists then
		if towerExists.TowerConfig.Price.Value <= player.Cash.Value then
			return true
		end
	elseif heroTowerExist then
		if heroTowerExist.TowerConfig.Price.Value <= player.Cash.Value then
			return true
		else
			warn("ur too poor lol")
		end
	else
		warn("This tower does not exist")
	end
	
	return false
end
requestTowerFunction.OnServerInvoke = tower.CheckSpawn

return tower

In terms of solutions, i’ve only tried to think what would the script do, I do have a idea but scripting it is near impossible… this is why i’m asking for help

If i’m doing those scripts is for a tower defense game, where they would be two towers types, normal and heroes (similar to area tower defense or BTD6)

Edit : Forgot to mention that I follow Gnome Code scripting series on how to make a Tower Defense game lol XD

picture with the childrens of the folders XD (such a dumbass I am)
image

2 Likes

maybe the folder “Guide” is nil, or some script is trying to remove the folder

1 Like

Can you tell us which part of the code is causing the error?

1 Like

the model “Guide” is not under Folder “ReplicatedStorage.TowersFolder”, check for casing maybe it’s lowercase and you just forgot about it.

1 Like

The model “Guide” is inside a folder where the Tower Folder is, no clue if it’s the best solution but i felt like it was easier for me (just so I don’t have folder scattered around)

line 61 of script module “TowerScript”

local newTower = ReplicatedStorage.TowersFolder[name]:Clone()

“Guide” is a model inside the folder where the model of towers are found
Basically guide is consider as another type of tower so I wanted to make a distinction in the script by putting in a folder (sorry if it sounds confusing)
(look for the comment above with the image I linked, I forgot to add it in my help topic)

1 Like
function tower.SpawnTower(player, name, cframe)
	local allowedToSpawn = tower.CheckSpawn(player, name)
	
	if allowedToSpawn then
		local newTower = ReplicatedStorage.TowersFolder[name]:Clone() -- the part which is causing the error
		
		newTower.Parent = workspace.CurrentPlacedTowers
		newTower.HumanoidRootPart:SetNetworkOwner(nil)
		newTower.HumanoidRootPart.CFrame = cframe
		
		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.Cash.Value -= newTower.TowerConfig.Price.Value
		if newTower.TowerConfig.IsAHero.Value == true then
			player.PlacedHeroTower = true
		end
			
		coroutine.wrap(tower.AttackEnemy)(newTower, player)
	else
		warn("Requested tower does not exist:", name)
	end
end

the part I commented should be the issue, also put the guide to the TowersFolder

image

that picture you showed shows that it’s in the HeroesTowers instead of TowersFolder

1 Like

This is indeed the line that is causing the error, I want the script to loop through both files
Also If the “Guide” is in a seperated folder inside the TowerFolders, it’s cuz i’m counting on making his upgrade system to the one in BTD6 or Arena Tower Defense

I guess as of now, I’ll leave it in the TowersFolder just for testing until I get the script to work

1 Like

Actually I’m dumb, checking my script, it already does the distinction between which tower is a hero or not… well anyway thank for your guys help.
Feel free to give me better solutions though ^^’

1 Like

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