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)