alright so i have a function that returns a value after doing a lot of stuff, and i want the variable that it returns in another script, so i call it like this:
local tower = parentModule.Spawn()
but the issue is that this that this also runs the spawn function, which i dont want i only want what it returns
yea i tried but then the variable will be the function and not what it returns, i checked with a print statement and it prints function: 0x118206612bc89734
also i forgot to mention this and its probably important but the spawn function has 4 parameters, but i cant give it those parameters because i am just trying to get what newTower is
Can you not call the function from that script, since it looks like its a function out of a module script? Else, why can’t you just run the function and send over the values?
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
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
I mean you can run the function, get what it returns then send that
for example, say this function returns max and min health you, can run it and save what it returns, then send those values to the other script
local a,b = functionsModule.GetMaxAndMinHealth()
--send a and b
local tower = parentModule.Spawn(mode)
[ now in the Spawn function you put a … ]
if mode == true then — do the spawn normaly
else — just send back the value
end
Think about what you’re saying here. You want to get the result of this function—which relies on a certain input—without providing any input and without it ever running. The way you’re going about this just won’t work.
Take a step back and explain why you need this, and we may be able to find a better way to structure this. Generally, ModuleScripts never need to both require each other cyclicly.
Just based on that sentence, I’d imagine a better approach would be to pass newTower as a parameter to a function in the other ModuleScript. Any code outside of a function that needs to rely on newTower should be moved into said function.