Why is this returning a function not an instance?

So I have this peice of code that is returning a function instead of an instance and I dont know why please help me here is the code:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local LocalPlayerVehicle = function()
	for Number, Instance2 in pairs(workspace["Guns"]:GetChildren()) do
		local Owner = Instance2.State:FindFirstChild("Owner")
		if Owner and Owner.Value == LocalPlayer then
			return Instance2
		end
	end
end

That returns function not AR1

Can you show the rest of the script?

Nothing in that code could possibly return a function. The function you have can only return either an instance (since GetChildren() always generates an array of instances) or nil (if your condition inside the loop is never true).

1 Like

Maybe you wrote LocalPlayerVehicle instead of LocalPlayerVehicle() when you called your function?

2 Likes

Are you sure the instance of ‘LocalPlayer’ is stored inside of the ‘Owner’ value?

I believe you are referencing LocalPlayerVehicle which is a function.

LocalPlayerVehicle = a function
LocalPlayerVehicle() = Instance2

Make sure you run the function and have those parenttheses.

local LocalPlayerVehicle = function()
-- ...
end

LocalPlayerVehicle is a function.
To get an Instance out of it, which is what it seems to be supposed to return, you have to call the function.

local LocalPlayerVehicle = (function()
-- ...
end)() -- define an anonymous function and immediately call it

Alternatively, leave LocalPlayerVehicle undefined and then run the code to populate it.

local LocalPlayerVehicle
for Number, Instance2 in pairs(workspace["Guns"]:GetChildren()) do
	local Owner = Instance2.State:FindFirstChild("Owner")
	if Owner and Owner.Value == LocalPlayer then
		 LocalPlayerVehicle = Instance2
		 break -- stop running the rest of the loop as return would
	end
end