Unable to return values from functions

hello guys, im trying to get some bool values of the player on a server script because im making a mission system, to do that im trying to indentify them using the player added event, then wrap it all on a function, and return the values, the problem is that is always returning nil, no matter what

local function getValues()
			game.Players.PlayerAdded:Connect(function(player)
				player.CharacterAdded:Connect(function(character)
					local folder = character:WaitForChild("Missions")
					local v = folder[missionFolder.Name]

					local onMission = folder.OnMission
					local enabled = v.Enabled
					local running = v.Running
					local completed = v.Completed
					
					local lol = 5
					
					return onMission, enabled, running, completed, lol
				end)
			end)
		end
		
		local onMission, enabled, running, completed, lol = getValues()
		
		print(lol)

when i print it inside the function it works btw

When you’re calling getValues(), it is registering your code with the PlayerAdded and CharacterAdded events and then exiting. When it exits at this time, it will return nil.

You would need to do something more like this:

local function getValues(character)
  local folder = character:WaitForChild("Missions")
  local v = folder[missionFolder.Name]

  local onMission = folder.OnMission
  local enabled = v.Enabled
  local running = v.Running
  local completed = v.Completed

  local lol = 5

  return onMission, enabled, running, completed, lol
end

game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function(character)
    local onMission, enabled, running, completed, lol = getValues(character)
    print(lol)
  end)
end)

man i need to get the values out of any event or function, because i will need to use them later on, if i reference them on the player added it will cause unnecessary lag

changed category, i accidentally put it on the wrong one

image
its now giving an error

Which line is giving the error? Did you pass “character” to the function? Screenshot doesn’t provide enough info.

If you want to try to avoid lag, maybe you can use the new task.delay() and also use Attributes. Something like:

local function getValues(character)
  task.delay(function(character)
    local folder = character:WaitForChild("Missions")
    local v = folder[missionFolder.Name]

    local onMission = folder.OnMission
    character:SetAttribute("Enabled", v.Enabled)
    character:SetAttribute("Running", v.Running)
    character:SetAttribute("Completed", v.Completed)
  end)
end

game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function(character)
    getValues(character)
  end)
end)

Attributes can be used to set values and retrieve them later.
(You would need to figure out the details depending on your needs.)