Remote Event Handler Doesn't Fire

I have an issue in my tycoon game. I have this button that people press to begin servicing customers. Whenever they leave in the middle of servicing a customer, the customers stop spawning in. I have a remote event handler that is supposed to run the function to spawn in a customer but it is not firing anymore. I’ve tried using a bindable event instead, and I’ve tried disconnecting it from the function and then reconnecting it. Nothing works and I am at a loss.

Here is my server script. And yes the clickedYes argument is always true.

beginWorkEvent.OnServerEvent:Connect(function(player, bank, clickedYes)
	if clickedYes then
		generateNpc(player, bank)
	end
end)

Here is the client script that fires the event.

beginButton.Activated:Connect(function()
	local bankName = Players.LocalPlayer.Bank.Value
	local bank = workspace.Banks:FindFirstChild(bankName)
	clickSound:Play()
	beginWorkEvent:FireServer(bank, true)
	gui.Visible = false
end)

Once a player uses the event and then leaves, the event gets disconnected. I need to make a separate event for each player when they join the game but doing the PlayerAdded doesn’t quite work. I’m not sure how to go about it.

5 Likes

Assuming the gui becomes invisible, I’m gonna need you to place some prints in the server script to see where the code stops. Try it like this:

beginWorkEvent.OnServerEvent:Connect(function(player, bank, clickedYes)
    print(clickedYes)
	if clickedYes then
        print(1)
		generateNpc(player, bank)
	end
    print(2)
end)

If 1 prints but your npc doesn’t generate then it should be a problem with the function itself

3 Likes

Yeah I already did that. None of the print statements run. The event handler is not running at all.

1 Like

Does putting a print before the FireServer part work? I’m not seeing any obvious problem here

1 Like

Yeah it does. Like I said above, I have another server script that picks up the event, which always works just fine. At first everything works perfectly, but if a player leaves in the middle of the event handler function that I provided above, the event handler doesn’t run anymore.

I’m pretty confused on what the problem could be. It might depend on how and where the connection is set up and what not

its because you cant send instances over remote events

you have to serialize them into strings and then unserialize them on the server

edit: use serialize() on the instance on the client and then send that to the server. the server then gets the instance with deserialize()

heres a module that can do those things:

local function tokenize(inputstr)

	local args = {}
	for str in string.gmatch(inputstr, "([^".. "." .."]+)") do
		table.insert(args, str)
	end

	return args

end


return {
	serialize = function(instance: Instance)
		return instance:GetFullName()
	end,
	deserialize = function(path)
		
		if not path then return false end
		
		local currentParent = game
		for _, childName in tokenize(path) do
	
			local child = currentParent:FindFirstChild(childName)
			if not child then return false end
			
			currentParent = child
			
		end
		
		return currentParent
		
	end,
}

Tried it, that’s not it. It works the first time too. Didn’t know I couldn’t send instances through remote events though, I’ve been doing that throughout my entire game and it seems to work fine. I could easily just send the name of the bank and then find it in the handler though, so it wouldn’t be a difficult fix.

Looks like you can send instances too. Remote Events and Callbacks | Documentation - Roblox Creator Hub

1 Like

You can’t send client-created instances to the server

1 Like

It was not client created, it’s an instance made by the server. I find the name of the bank from a value stored in the player, and then I get the actual bank from a folder. It works the first time around and in all the other functions I pass an instance. I also tried just passing the string name of the bank and then finding it in the event handler function but that does not work. I’ve looked more so into the function and I’ve found that I have a MoveToFinished:Wait() thing so I believe it’s getting held up at that part. So, I need to find a sort of time out thing I can implement into the function.

MoveToFinished times out after 8 seconds by default, I think. It’s worth investigating though. I looked and tested in Studio myself with a remote and 2 connections and that’s for sure not the issue. I’m troubled

2 Likes

I went ahead and added the function that’s run by the handler. I threw in a bunch of print statements and found that it seems to consistently get stuck after the third and final MoveTo(). Not sure why though. There are no errors or anything at all after the player leaves and the npc is destroyed.

1 Like

I did some more digging, and the problem is bigger. It has nothing to do with the generateNpc function and it doesn’t just happen to that specific bank. When a player uses the event and then leaves it breaks all together. I asked the assistant ai and it said that I could make a separate event handler for each player but I’m not quite sure how to do that.

Still not quite sure exactly why it was breaking but I worked around it. I completely rewrote the script inside of the other script that was working and now it works just fine. Since the event handler wasn’t working I just took it out and reworked it. Nightmares but I solved it sort of :man_shrugging: