Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why isn't my remote event firing?

Hello! I am trying to make a UI to change teams when a button is pressed. I have a "for i, v in pairs()" loop to cycle through all of the buttons, and when v is pressed, it should fire a remote event with the value of an object value in the button (v) that was pressed. I know the clicking script works because I prints "clicked" when I press a button, but on the normal script that fires when the event is fired, there is no output in the console, though it should print "fired" when the event is fired. Sorry if I'm bad at explaining, but there are code snippets below that should explain my error.

The script that fires the event when a button is pressed

local frame = script.Parent

b = {
	frame.Medics;
	frame.Class_D;
	frame.Chaos_Insurgency;
	frame.Scientists;
	frame.Security;
	frame.Mobile_Task_Force;
}

for i, v in pairs(b) do
	v.MouseButton1Click:Connect(function()
		print("clicked")
		game:GetService("ReplicatedStorage").Events.ChangeTeam:FireServer(v.Team.Value)
	end)
end

the script that fires when the event is fired

local event = game:GetService("ReplicatedStorage").Events.Sirens

event.OnServerEvent:Connect(function(player, team)
	print("fired")
	if team ~= nil then
		if team:IsA("Team") then
			player.Team = team
		else
			warn("Argument 2 is not a team model. Code did not compile.")
		end
	else
		warn("Argument 2 missing or nil. Code did not compile.")
	end
end)

when i press a button, "clicked" is put in the output, but "fired" isn't. This is my issue, the event isn't getting fired for whatever reason. There are no errors in the output

1 answer

✓ Accepted answer

line 15 in the LocalScript.

 game:GetService("ReplicatedStorage").Events.ChangeTeam:FireServer(v.Team.Value)

line 1 and 3 in the ServerScript

local event = game:GetService("ReplicatedStorage").Events.Sirens

event.OnServerEvent:Connect(function(player, team)

It didn't work because it's not the same RemoteEvent. So to fix this just change line 1 in the ServerScript:

local event = game:GetService("ReplicatedStorage").Events.ChangeTeam

event.OnServerEvent:Connect(function(player, team)

In the LocalScript that table just makes your script long to shorten it use :GetChildren().

Remember this, pairs are used for dictionaries, and ipairs are used for arrays. :GetChildren returns a table specifically an array.

local frame = script.Parent

for index, value in ipairs(frame:GetChildren()) do
    value.MouseButton1Click:Connect(function()
        print("clicked")
        game:GetService("ReplicatedStorage").Events.ChangeTeam:FireServer(value.Team.Value)
    end)
end

1 comment

0

Thanks! It's like 12:15 AM and im brain dead so it would of taken me a while to find that on my own. thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.