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
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).
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