BindableEvent returns nil

My BindableEvent from ServerScriptService Script -> BindableEvent -> Workspace script returns nil even though the returned variable is defined. I can guarantee that as the variable has been pre-checked in a print() before being sent. But once the server script receives it, it suddenly becomes nil.


-- Workspace "MapHandler"
		map = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("EndMapVote"):Fire()
		if map == nil then 
			print("map was nil: ")
			print(map)
        end
-- Serverscriptservice "MainScript"
game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("EndMapVote").Event:Connect(function()
	print("chosen map at return: ")
	print(mapName)
	print("returned map: ")
	print(script.Map.Value)
	return script.Map.Value
end)

Thanks in regards!

1 Like

I think you may need to use a BindableFunction rather than a BindableEvent in this case since you want a return value

2 Likes

I didn’t even know that existed! Sorry for wasting time, I’ll solute this once I’m back from changing the code. :slight_smile:

1 Like

The difference between BindableEvents and BindableFunctions is that BindableFunctions act something like functions, and BindableEvents act a bit like functions except the fact that you do not await for a response.

To clarify however, you must always return with a BindableFunction as otherwise it infinitely stalls and BindableFunctions cannot be ‘connected’ to, it instead expects a function as an input.

So if I read your replies correctly, the purpose of any sort of Event is to get fired (with or without parameters) from somewhere, while any sort of Function is to get fired (with or without parameters) from somewhere AND return one or more variables.

Thank you all for your help!

Seems like I’ve run into an error.
image

I changed it to .OnInvoke:Connect(function(), was this wrong?
And I changed the other end to :Invoke()

Could you please show the full (at least enough for context, like in your main post) code?

game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("EndMapVote").OnInvoke:Connect(function()
	print("chosen map at return: ")
	print(mapName)
	print("returned map: ")
	print(script.Map.Value)
	return script.Map.Value
end)

and

		map = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("EndMapVote"):Invoke()
		if map == nil then 
			print("map was nil: ")
			print(map)
		end	

The way you set OnInvoke in your code is like an event, this is how you do it for BindableFunctions

Taken from the developer wiki as an example, treat OnInvoke as a variable that you assign a function to

image

1 Like
game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("EndMapVote").OnInvoke = function()

so like this instead?

A BindableFunction’s OnInvoke is like an the parameter of the type function of an object. You have to set it, you cannot connect to it like an event.

Yes, so it shoudl be like this at the end

game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("EndMapVote").OnInvoke = function()
	print("chosen map at return: ")
	print(mapName)
	print("returned map: ")
	print(script.Map.Value)
	return script.Map.Value
end

The Invoke should just be a simple replace from Fire to Invoke, no changes required there

I’ll try that out. Thanks for your patience.

1 Like

It appears to be working! Just so I don’t create too many posts, is it possible you could help me with this related line of code?

		local mapname = string.gsub(map, "_", " ")
		local mapname = string.gsub(map, "And", "&")
		print(mapname)
		choosemap = ReplicatedStorage:WaitForChild("Maps"):FindFirstChild(mapname)
		if ReplicatedStorage:WaitForChild("Maps"):FindFirstChild(mapname) == nil then
			print("could not find given map: ".. mapname)
		end

it keeps not finding the given map because the _ and the And never gets replaced. Let me give you an example:

  1. Default_Map has been assigned to mapname
  2. Default_Map should change to Default Map but never does.
  3. Script returns "Could not find given map: Default_Map".

Thanks in regards. Don’t worry, I won’t prioritize this 'cause the subject is something else.
:slight_smile:

You’re reassigning mapname twice and not using mapname for the gsub second

local mapname = string.gsub(map, "_", " ")
mapname = string.gsub(mapname, "And", "&")
print(mapname)
choosemap = ReplicatedStorage:WaitForChild("Maps"):FindFirstChild(mapname)
if not ReplicatedStorage:WaitForChild("Maps"):FindFirstChild(mapname) then
	print("could not find given map: ".. mapname)
end

You declare ‘mapname’ twice, and overwrite it the second time

local mapname = string.gsub(map, "_", " ")
mapname = string.gsub(mapname, "And", "&")

Wow, thank you both for answering at such a reliably consistent speed! I wish I could solute you both as you pretty much point out the same things but differently formulated each time.

It all worked in the end, so thank you both for participating in this case!
-Mathe

2 Likes