there is another function and it has newTower as a parameter, so maybe this could be used?
function tower.Attack(newTower,player)
local config = newTower.Config
local target = tower.FindTarget(newTower, config.Range.Value, config.TargetMode.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
events:WaitForChild("AnimateTower"):FireAllClients(newTower, "Attack", target)
local module = require(script.Gunner)
module.Attack()
if target.Humanoid.Health <= 0 then
player.Money.Value += target.Humanoid.MaxHealth
player.Kills.Value += 1
end
task.wait(config.Cooldown.Value)
end
task.wait(0.1)
if newTower and newTower.Parent then
tower.Attack(newTower,player)
end
end
basically, tower gets spawned, sends the newTower parameter to the attack function, but what i want is to have different module scripts for each tower, so i need to get the newTower variable either from the spawn or the attack function in the seperate module script
function tower.Attack(newTower,player)
local config = newTower.Config
local target = tower.FindTarget(newTower, config.Range.Value, config.TargetMode.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
events:WaitForChild("AnimateTower"):FireAllClients(newTower, "Attack", target)
target.Humanoid:TakeDamage(25)
if target.Humanoid.Health <= 0 then
player.Money.Value += target.Humanoid.MaxHealth
player.Kills.Value += 1
end
task.wait(config.Cooldown.Value)
end
task.wait(0.1)
if newTower and newTower.Parent then
tower.Attack(newTower,player)
end
end
function tower.Spawn(player, name, cframe, previous)
local allowedToSpawn = true --tower.CheckSpawn(player,name)
if allowedToSpawn then
local newTower
local oldTargetMode = nil
if previous then
oldTargetMode = previous.Config.TargetMode.Value
previous:Destroy()
newTower = game.ReplicatedStorage.Towers.Upgrades[name]:Clone()
else
newTower = game.ReplicatedStorage.Towers[name]:Clone()
end
bindables:WaitForChild("GetTower"):Fire(newTower)
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 = oldTargetMode 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
object.CollisionGroup = "Tower"
end
end
player.Money.Value -= newTower.Config.Price.Value
coroutine.wrap(tower.Attack)(newTower, player)
return newTower
else
warn("no tower ["..name.."]")
return false
end
end
so there are the two functions, and in the attack function there is this:
target.Humanoid:TakeDamage(25)
but instead of the target just taking damage, i would want it to go to an entirely different module script and do it there for each tower
in the attack function there is a newTower parameter which is the tower that gets spawned. this is received from the spawn function
but if i want to do the damage in a different modulescript, it doesnât know what newTower is there
function tower.GetByName(name)
for i in pairs(activeTowers) do
if i == name then
return activeTowers[i]
end
end
end
function tower.GetAll()
return activeTowers
end
Then another module script could require your tower module and do:
for _,tower in pairs(tower.GetAll()) do
tower:TakeDamage(25) -- whatever
end
Hello, I think this is what you want to achieve (I might get your question wrong)
function tower.CloneTower(name)
local newTower = game.ReplicatedStorage.Towers[name]:Clone()
return newTower
end
You can then call this function to get a cloned newTower object without executing the full tower.Spawn() function. However, do note that this will only clone the tower from ReplicatedStorage and will not have any of the additional setup (like ownerValue , targetMode , bodyGyro , and so on) performed in tower.Spawn() . So, if you need that setup, youâll need to implement it separately.
hi, so this worked great up until today, where i found out that it doesnt work when i upgrade towers
when i upgrade towers in the seperate module script, it tells me that config is not a valid member
iâve found out the reason for this is because when i upgrade, the seperate module script thinks the tower is still the previous level, which gets deleted when you upgrade
the upgrading is done in the spawn function on this line:
if previous then
oldTargetMode = previous.Config.TargetMode.Value
previous:Destroy()
newTower = game.ReplicatedStorage.Towers.Upgrades[name]:Clone() -- done here
if anyone can help that would appreciated, even with a different method