Need help returning a value from a function

Hi, below is my script where I’m using a remote event to fire the chosenMap to the client(which is a value). I’m sure the remote event is working properly because I’ve already tested it in the output. My problem is trying to return the chosenMap value and use it in my loadMap() function. When I call the function it returns a blank value. I’ve tried messing with the syntax but got nothing to work. Any help would be appreciated.

local function returnChosenMap()
	local winner = CalculateWinner()
	if winner == nil then
		local chosenMap
		RandomMap.OnClientEvent:Connect(function(chosenMap)
			return chosenMap
		end)
	end
end
function loadMap()
	local winner = CalculateWinner()
	print(returnChosenMap())
	local chosenMap = returnChosenMap()
	Intermission.Text = "Map Loading..."
	task.wait(UntilMapLoaded)
	Intermission.Visible = false
	local Character = player.Character or player.CharacterAdded:Wait()
	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
	if winner == "Map1" or chosenMap == 1 then
		HumanoidRootPart.CFrame = Maps.Map1.Spawn.CFrame + Vector3.new(0,3,0)
	elseif winner == "Map2" or chosenMap == 2 then
		HumanoidRootPart.CFrame = Maps.Map2.Spawn.CFrame + Vector3.new(0,3,0)
	elseif winner == "Map3" or chosenMap == 3 then
		HumanoidRootPart.CFrame = Maps.Map3.Spawn.CFrame + Vector3.new(0,3,0)
	end
	UntilRoundEnds = 180
	while task.wait(1) do
		MapTimer.Text = tostring(toMS(UntilRoundEnds))
		MapTimer.Visible = true
		UntilRoundEnds = UntilRoundEnds - 1
		MapTimer.Text = tostring(toMS(UntilRoundEnds))
		if UntilRoundEnds == 0 then 
			MapTimer.Visible = false
			roundEnded()
			EndOfRound:FireServer()
			return
		end
	end
end

RemoteEvents allow for 1 way communication (Client-> Server / Server → Client).

You should instead be using a RemoteFunction, which allows for 2 way communication (Client → Server → Client)


What are you actually attempting to acheive with the first function? Its a bit confusing and is likely flawed.

What would I need the remote function for? I’m only firing the choseMap value from server to client

I misinterpreted what you were attempting to achieve I think.

Can you please elaborate on what the returnChosenMap thing is meant to do?

Basically I have a map voting system with a separate function CalculateWinner() that will check which map has the most votes. But when there is no winner(the maps were tied or there were no votes) then I picked a random map(which is a value) on the server and fired it to the client. I’m trying to use the random map to teleport the player onto the map and I’m using the returnChosenMap() function to use the random map value in my loadMap() function

Sorry my script is pretty messy and confusing.

You can do that with a math.random(1, #Maps) and just return that number.

I know but I had to do that on the server since every client would get a different value and be teleported to a different map

remote events will not return a value, you would need to use a remote function for that.

That is correct. Most things like this that affect all clients should be done on the server. You can then :FireAllClients(mapNumber) or something