How to return a variable so I can use it somewhere else?

Hi everyone, so I’m making a function that creates a rope, and applies a force to you when it does create a rope, and I’m trying to make it so that it tells you what the force is so I can use it for later out of the function. I hope that made sense lol.

Would I use return and if so, how would I use it for my case? Here is the code (only part of it):

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local Rope = Instance.new("SpringConstraint")
local RunService = game:GetService("RunService")

local VF = Instance.new('VectorForce')
local target = nil
local isholdingdown = false

function createRope(target, mousepos, isholdingdown)
	if isholdingdown == false then
		VF.Force = Vector3.new(0, 0, 0)
		Rope:Destroy()
		return
	elseif (char.HumanoidRootPart.Position - mousepos).Magnitude > 1000 then
		return
	elseif isholdingdown == true then
		local A0 = Instance.new('Attachment')
		local A1 = Instance.new('Attachment')
		A0.Parent = workspace.Terrain
		A0.WorldPosition = mousepos
		A1.Parent = player.Character.HumanoidRootPart
		Rope = Instance.new("SpringConstraint")
		Rope.Attachment0 = A0
		Rope.Attachment1 = A1
		Rope.Parent = player.Character:WaitForChild("RightHand")
		Rope.Visible = true
		Rope.LimitsEnabled = true
		Rope.Coils = 0
		Rope.Thickness = 0.1
		Rope.Color = BrickColor.new("Institutional white")
		Rope.MaxLength = (player.Character:WaitForChild("HumanoidRootPart").Position - target.Position).Magnitude
		VF.Name = "SpiderForce";
		VF.Attachment0 = player.Character.PrimaryPart.RootRigAttachment;
		VF.Parent = player.Character.PrimaryPart;

		--Detect using state changed

		player.Character:WaitForChild("Humanoid").StateChanged:Connect(function(_, state)
			if state == Enum.HumanoidStateType.Landed or state == Enum.HumanoidStateType.Physics then
				VF.Force = Vector3.new(0, 0, 0)
				print("Landed and successfully set VectorForce to 0!")
			elseif state == Enum.HumanoidStateType.Freefall or state == Enum.HumanoidStateType.Flying then
				VF.Force = Vector3.new(-1000, -2000, -3000);
				print("Flying and applying force.")
			end
		end)
	end
end

I want to return VF.Force so I can use it for later.

1 Like

The problem with this is that you actually create a connection in the function so returning a value would only return it in your connection, not your actual function. You can though just use VF.Force later in your script anywhere.

1 Like

You usually put return at the end of functions or when a condition is met to return a value that ends the function early

2 Likes

What do you mean to create a connection?

And also @DasKairo if I do put it at the end of the function, how would I access it?

Here is an Example:

function Add(x, y) -- function with 2 Arguments
    return x + y -- returns Addition
end

local Data = Add(3, 6) -- Variable gets Data from function (fires it)
print(Data) 
--> 9

Its a Very simple Example.

2 Likes

You are making a connection to the StateChanged event here, so if you return anything in the function after the :Connect() then you will not get the value that you are wanting. Since you change the value of the VF in that connection also, your value for the VF.Force will not always be the one that you think you have. And, every time that you call the createRope() function you are making a new connection to the StateChanged, and the same function will run every time the state changes, creating a memory leak.

1 Like

You’ve already stored VF variable


just use VF.Force anywhere. (inside the same script)

example:

local VF = Instance.new("VectorForce")

function createRope()
    VF.Force = Vector3.new(7,7,7)
end
print(VF.Force) -- 7,7,7
1 Like

To return the value of VF.Force , you just need to add return VF.Force at the end of the function createRope . This will return the value of VF.Force when you call the function.

Here’s the updated code:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local Rope = Instance.new("SpringConstraint")
local RunService = game:GetService("RunService")

local VF = Instance.new('VectorForce')
local target = nil
local isholdingdown = false

function createRope(target, mousepos, isholdingdown)
	if isholdingdown == false then
		VF.Force = Vector3.new(0, 0, 0)
		Rope:Destroy()
		return
	elseif (char.HumanoidRootPart.Position - mousepos).Magnitude > 1000 then
		return
	elseif isholdingdown == true then
		local A0 = Instance.new('Attachment')
		local A1 = Instance.new('Attachment')
		A0.Parent = workspace.Terrain
		A0.WorldPosition = mousepos
		A1.Parent = player.Character.HumanoidRootPart
		Rope = Instance.new("SpringConstraint")
		Rope.Attachment0 = A0
		Rope.Attachment1 = A1
		Rope.Parent = player.Character:WaitForChild("RightHand")
		Rope.Visible = true
		Rope.LimitsEnabled = true
		Rope.Coils = 0
		Rope.Thickness = 0.1
		Rope.Color = BrickColor.new("Institutional white")
		Rope.MaxLength = (player.Character:WaitForChild("HumanoidRootPart").Position - target.Position).Magnitude
		VF.Name = "SpiderForce";
		VF.Attachment0 = player.Character.PrimaryPart.RootRigAttachment;
		VF.Parent = player.Character.PrimaryPart;

		--Detect using state changed

		player.Character:WaitForChild("Humanoid").StateChanged:Connect(function(_, state)
			if state == Enum.HumanoidStateType.Landed or state == Enum.HumanoidStateType.Physics then
				VF.Force = Vector3.new(0, 0, 0)
				print("Landed and successfully set VectorForce to 0!")
			elseif state == Enum.HumanoidStateType.Freefall or state == Enum.HumanoidStateType.Flying then
				VF.Force = Vector3.new(-1000, -2000, -3000);
				print("Flying and applying force.")
			end
		end)
	end
	return VF.Force
end 

Now, when you call the function createRope , the returned value will be assigned to a variable, which you can use later in your code. For example:

local force = create
1 Like

I used that as a workaround before creating this post just for the time being lol because it arises some problems that would be better just to use return

How would I get the force? That’s the part I’m most confused about. It seemed like you were trying to do it on the last line but it got cut off.