Having trouble with interscript RemoteEvent connections

I am having trouble connecting a RemoteEvent firing in one script to a RemoteEvent firing in another script. This is a sort of follow-up to my last post.

This part of the code is meant to fire the event on the given parameter:

local HairColorButtonArray = workspace.SettlerLobby.ScriptedObjects.MasterCCDisplay.MainDisplay.GuiMasterParent.HairSelectionMainframe.HairColors:GetChildren()
local ClickSound = workspace.SettlerLobby.ScriptedObjects.MasterCCDisplay.MainDisplay.Click
local SettlerLobbyEvents = game.ReplicatedStorage.LobbyEvents.SettlerLobbyEvents

for i , v in pairs(HairColorButtonArray) do
	if v:IsA("ImageButton") then
		v.MouseButton1Click:Connect(function()
			ClickSound:Play()
			if v.Name == "01HairColor" then
				SettlerLobbyEvents.HairColorEvent:FireServer("01HairColor")
			end
			if v.Name == "02HairColor" then
				SettlerLobbyEvents.HairColorEvent:FireServer("02HairColor")
			end
		end)
	end
end

This part of the code is the reciever, meant to detect the parameters and take action given the parameter:

local HCServerEvent = game.ReplicatedStorage.LobbyEvents.SettlerLobbyEvents.HairColorEvent

local Char = game.Players.LocalPlayer.Character
local player = game.Players.LocalPlayer

HCServerEvent.OnServerEvent:Connect(function(player, event)
	if event == "01HairColor" then
		print("Debug 01HairColor")
	end
	if event == "02HairColor" then
		print("Debug 02HairColor")
	end
end)

I’m getting no error messages via the devlog. Please help!

Are both of these local scripts on the client?

The second script appears to be a script on the server, but you are attempting to index a LocalPlayer property which only exists on the client.

The second script is a Script, the first one’s a LocalScript.

1 Like

The LocalPlayer property is not replicated to the server (scripts) and can only be used on the client (local scripts). Right now, the server script will throw an error and stop executing when attempting to index the Character property of the LocalPlayer as it will always be nonexistent on the server.

Try removing all references to the LocalPlayer property on the server as such:

local HCServerEvent = game.ReplicatedStorage.LobbyEvents.SettlerLobbyEvents.HairColorEvent

HCServerEvent.OnServerEvent:Connect(function(player, event)
	if event == "01HairColor" then
		print("Debug 01HairColor")
	end
	if event == "02HairColor" then
		print("Debug 01HairColor")
	end
end)

Tried it, still no dice. The problem is most likely with the receiver, not the event firing part (as you probably know), and the 2nd bit of the script is in ServerScriptService

1 Like

Does the clicksound play, aka the mousebutton event is set up correctly?

Yes. The first part of the script works flawlessly, it’s the second part I’m having issues with.

Like @UnderMyWheel said .LocalPlayer is not replicated to the server so your server script wouldn’t work anyways,

You’re looping through the buttons and doing v.MouseButton1Click and then checking for the name…

try printing v.Name ( also I don’t really recommend what you’re doing ).

1 Like

You don’t need to detect the player as a RemoteEvent does that anyway.

    local HCServerEvent = 
    game.ReplicatedStorage.LobbyEvents.SettlerLobbyEvents.HairColorEvent

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

        local Char = player.Character

    	if event == "01HairColor" then
    		print("Debug 01HairColor")
    	end
    	if event == "02HairColor" then
    		print("Debug 02HairColor")
    	end

    end)
1 Like