Problem firing button events in RemoteEvent

I am implementing a system that has multiple buttons with seperate events in a RemoteEvent. However, only the first event fires when clicked.

Code:

-- Define values
local queue = {}
local player = game:GetService("Players").LocalPlayer

-- Define services
local TweenService = game:GetService("TweenService")

-- Define tween variables
local Notification = script.Parent.Notification

local HidePosition = UDim2.new(0.113, 0, -0.13, 0)

local ForceHideTweenInformation = TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.InOut, 0, false, 0)

local HideProperties = {["Position"] = HidePosition}

-- Runs when remote event is fired
game.ReplicatedStorage.RemoteEvents.Notify.OnClientEvent:Connect(function(TitleText, DescriptionText, interval, delaytime, PlaceID)
	-- Clone notification and set text
	local NewNotification = Notification:Clone()
	local ClearQueue = NewNotification.ClearQueue
	local TeleportButton = NewNotification.Teleport
	
	table.insert(queue, NewNotification)
	NewNotification.Title.Text = TitleText
	NewNotification.Description.Text = DescriptionText
	NewNotification.Parent = Notification.Parent
	
	if PlaceID ~= nil then
		TeleportButton:SetAttribute("PlaceID", PlaceID)
	else
		TeleportButton:Destroy()
	end

	local ForceHideTween = TweenService:Create(NewNotification, ForceHideTweenInformation, HideProperties)
	
	NewNotification.Exit.Activated:Connect(function()
		ForceHideTween:Play()
		ForceHideTween.Completed:Wait()
		table.remove(queue, table.find(queue, NewNotification))
		NewNotification:Destroy()
	end)
	
	ClearQueue.Activated:Once(function()
		table.clear(queue)
		print("queue cleared for " .. player.Name)
	end)
	
	TeleportButton.Activated:Connect(function()
		local TeleportService = game:GetService("TeleportService")
		TeleportService:Teleport(PlaceID, player)
	end)	
end)

Any help? No errors appear in the output, and I have no idea why this happens.

Create three functions above that remote event, outside that function. You have a nesting problem. just do local function for each one. Infact you can take it a step further and create local functions and call those functions to each remote event. It doesn’t all need to be in one function.
Example:

local function TeleportHandler()
		local TeleportService = game:GetService("TeleportService")
		TeleportService:Teleport(PlaceID, player)
end)
TeleportButton.Activated:Connect(function()
TeleportHandler()
end)

Once a remote event returns something to the server it stops, the way client and server talk once it finds a correct value for that function it returns it. This is why you seperate them all then call all functions, it will go through each function.

Here is a random example form my pet system:

RemoteEvents.UnequipPet.OnClientEvent:Connect(function(uuid: string)
	task.delay(0, function()
		UpdatePet(uuid)
		UpdateStorage()
		UpdateTotal()
	end)
end)

This won’t work, as the events are nested in the RemoteEvent as the new notification is cloned each time, therefore needing correct parameters. They need to all be seperate, but run in the same RemoteEvent.

Also, I don’t think the RemoteEvent ends when an event runs. I have other segments of code that use Tween.Completed:Wait() that I haven’t included in the post that run after, as they are not important.

Does the tween actually complete? Because it’ll keep yielding otherwise.

This is not the issue with the code. This code runs afterwards, so it would have no effect on the above code anyways.

So you already tried print debug?

Yes. The events do not fire. This is the reason why I posted this in the first place.

Fyi if PlaceID is ever nil, you will have an error because TeleportButton.Activated will error because TeleportButton is destroyed.

As for what your issue is… What exactly do you mean when you say “only the first event fires when clicked”? What is “the first event” and what are you clicking out of these three buttons?

The first event (Exit.Activated) is the only event that fires when clicked. None of the other buttons do anything.

Also, PlaceID will never be nil

Are you sure the others are actually Buttons and are set to Active / Enabled = true?

And if PlaceID will never be nil, why even have that if statement?

FOUND SOLUTION!!! I didn’t enable the ‘Active’ property

Yup, that’s what I thought haha. Glad you got it!

Actually, nevermind. It will be nil sometimes.

Okay. Then you need to make sure you don’t connect to the Activated event on TeleportButton when PlaceID is nil. Otherwise you will get an error.

You will still need to fix your code one day, what you are doing is very bad code practice DONT NEST FUNCTIONS!!!

I need to nest functions to pass the new notification as a parameter. If you have another way of doing this, share the knowledge.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.