Can i call a function without running it?

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

5 Likes

It always returns the same thing? If so, do the calculations when the script first starts and save it as a variable.

2 Likes

no it returns different things, its a variable for the tower that spawns

2 Likes

Try to remove brackets.
Like this:

local tower = parentModule.Spawn
2 Likes

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

2 Likes

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

2 Likes

although now that i think about i did give it nil parameters and it still ran the function so i dont think it matters

2 Likes

So do you just want to get the most recent thing that it created? You could save a variable in the table returned by that ModuleScript.

2 Likes

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?

2 Likes

i want what it returns, but if i call the function it runs it, and i don’t want it to run

im not sure what you mean by saving a variable since these are 2 different modulescripts

1 Like

here is the function i am calling:

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 want to get the newTower

1 Like

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
1 Like

Maybe try …

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

1 Like

sorry not sure what you mean, im trying to transfer the newTower value to the other scripts, not sure what you mean by mode

1 Like

i dont really understand, this is just calling the function in a variable which is what i was doing

1 Like

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.

2 Likes

“mode” is just a variable you can send with the call so you can use it as a logic switch in the spawn function.

local tower = parentModule.Spawn(true) — spawn normaly
local tower = parentModule.Spawn(false) — Just sends back value.

You will need to script out that part after the logic switch.

1 Like

yeah using returns isn’t needed, all i want to do is send that newTower value that is in the spawnfunction to another modulescript

1 Like

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.

2 Likes

if i put it as a parameter in the spawn function i would still have to call it which means it would run

1 Like