function tower.Spawn(player, name, cframe, previous)
local allowedToSpawn = tower.CheckSpawn(player, name, previous)
if allowedToSpawn then
local newTower
local oldMode = nil
if previous then
oldMode = previous.Config.TargetMode.Value
previous:Destroy()
newTower = ReplicatedStorage.Towers.Upgrades[name]:Clone()
else
newTower = ReplicatedStorage.Towers[name][name]:Clone()
player.PlacedTowers.Value += 1
end
local ownerValue = Instance.new("StringValue")
ownerValue.Name = "Owner"
ownerValue.Value = player.Name
ownerValue.Parent = newTower.Config
local targetMode = Instance.new("StringValue")
targetMode.Name = "TargetMode"
targetMode.Value = oldMode or "First"
targetMode.Parent = newTower.Config
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.Cash.Value -= newTower.Config.Price.Value
coroutine.wrap(tower.Attack)(newTower, player)
return newTower
else
warn("Requested tower does not exist", name)
return false
end
end
SpawnTowerFunction.OnServerInvoke = tower.Spawn
#no scripter in the world can help, join if your s2 or s3
59 messages · Page 1 of 1 (latest)
this is the tower.spawn function
this is the checkspawn function
function tower.CheckSpawn(player, name, previous)
local towerExists = ReplicatedStorage.Towers[name]:FindFirstChild(name, true)
if towerExists then
if towerExists.Config.Price.Value <= player.Cash.Value then
if previous or player.PlacedTowers.Value < maxTowers then
return true
else
warn("Player has reached max limit")
end
else
warn("Player can not afford the tower")
end
else
warn("The requested tower does not exist")
end
return false
end
requestTowerFunction.OnServerInvoke = tower.CheckSpawn
return tower
this is the invoke in the local script
gui.Selection.Action.Upgrade.Activated:Connect(function()
if selectedTower then
local upgradeTower = selectedTower.Config.Upgrade.Value
print(upgradeTower.Name ,selectedTower.HumanoidRootPart, selectedTower)
local upgradeSuccess = SpawnTowerFunction:InvokeServer(upgradeTower.Name, selectedTower.HumanoidRootPart.CFrame, selectedTower)
if upgradeSuccess then
selectedTower = upgradeSuccess
toggleTowerInfo()
end
end
end)
this is the error "Archer is not a valid member of Folder "ReplicatedStorage.Towers""
it occurs at line 182 which is this
local towerExists = ReplicatedStorage.Towers[name]:FindFirstChild(name, true)
and
line 206 in the local script which is this
local upgradeSuccess = SpawnTowerFunction:InvokeServer(upgradeTower.Name, selectedTower.HumanoidRootPart.CFrame, selectedTower)
all the variables, everything is correct
the only problem is
I HAVE NO IDEA WHERE THE HELL IT IS PUT "REPLICATEDSTORAGE.TOWERS"
Remove [name] here
but that just
results in errors
because
you need to find the name
folder
then the name
I know
that it is because of [name]
Show how towers are stored
because
inside the folder
is the model
with its viewportframe
the scripts access the model
but
when it comes
But
to accessing upgrades
it doesnt recognize the upgrades folder
if i put it in towers
errors again
If there is no folder [name] will error
:FindFirstChild(name)
hm
If there is no folder it will return nil
First find the folder of a tower, check if it exists and then do whatever you want
i just tried something
it checks if the folder exists first
huh
oh wait
uh
it looked for archer
inside archer
hm
local towerExists = ReplicatedStorage.Towers[name]:FindFirstChild(name, true)
In theory, what you are doing in this line is literally go to towers, access Name folder, and then, again, find for another object with it's name, recursively, why?
The error you are getting is probably because, as you are accessing the object straight by it's index, you get the error, in this case "Archer", when it doesn't exist
If you are trying to find "Archer", and you know it is in the first level, inside "Towers", the best option would be to access by it's name as you have done, but that may lead to errors when by unknown reasons, the object gets removed or renamed, failing and halting the script.
It's best to use FindFirstChild, or pcall the access
local Towers = ReplicatedStorage.Towers
local TowerFolder = Towers:FindFirstChild(name)
if TowerFolder then
-- tower exists
else
-- tower not exists
end
+1
^