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

Remote event doesn't work?

Hi there! I'm having a problem with a remote event, attached to a local script. Here is a script that assigns GUI to the player (on server) , when their character is loaded:

GUI = game.ReplicatedStorage.GUI:GetChildren()
		if #player.PlayerGui:GetChildren() < 2 then
			for i = 1,#GUI do
				GUI[i]:Clone().Parent = player.PlayerGui
			end
		else
			game.ReplicatedStorage.LocalScript.RemoteEvent:FireClient(player)				   	   		 	  		
end

...this works fine. I've tested it multiple times and, accordingly, the script works. The problem is that the local script (that assigns GUI to the client) doesn't fire the event:

script.RemoteEvent.OnClientEvent:Connect(function(player)
	GUI = game.ReplicatedStorage.GUI:GetChildren()
	for i = 1,#GUI do
		GUI[i]:Clone().Parent = game.Players.LocalPlayer.PlayerGui
	end
end)

Any help? I really appreciate it, thanks!

6 comments

0

^ How does it know what client to fire?

0

The fire client only works with the player parameter, I've tested it

0

I don't know what you mean by "how does it know what client to fire"?

0

Just to help you with organization, typically people put all RemoteEvents/Functions in the ReplicatedStorage, Local Scripts in the ReplicatedFirst or StarterPlayer, and Scripts in the ServerScriptService/Decendant of Workspace

0

My bad

0

You're good, no worries. Thanks for the help!

1 answer

✓ Accepted answer

###You do not include the player in OnClientEvent. This is because the server actually fires OnClientEvent, so the client doesn't need any player parameter since clients can use LocalPlayer. If an event passes the player by default, it's most likely a server-side only event! Though for FireClient, the first argument is the Player object the server will fire to. Also, LocalScripts do not run in ReplicatedStorage, they only run in PlayerGui, Backpack, PlayerScripts, the Character, and ReplicatedFirst.

local GUI = game.ReplicatedStorage.GUI:GetChildren() -- use local variables
    if #player.PlayerGui:GetChildren() < 2 then
        for i = 1,#GUI do
            GUI[i]:Clone().Parent = player.PlayerGui
        end
    else
        game.ReplicatedStorage.LocalScript.RemoteEvent:FireClient(player)                                       
end

###Client code

-- Place in StarterPlayerScripts
local rep = game:GetService("ReplicatedStorage")

rep.RemoteEvent.OnClientEvent:Connect(function(player)
    local GUI = game.ReplicatedStorage.GUI:GetChildren()
    for i = 1,#GUI do
        GUI[i]:Clone().Parent = game.Players.LocalPlayer.PlayerGui
    end
end)

3 comments

0

Thanks a lot!

0

I just plugged it in appropriately, as well as rewrote the code, and it still doesn't work. I'm trying to experiment with different local script parents...

0

Okay, the only thing that was wrong was that the server ran faster than the local script, so the server script needed a short wait time before running the script.

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