Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Remote event doesn't work?

Asked by 5 years ago

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!

0
^ How does it know what client to fire? LetterSlayer 42 — 5y
0
The fire client only works with the player parameter, I've tested it SnazzySpider67 53 — 5y
0
I don't know what you mean by "how does it know what client to fire"? SnazzySpider67 53 — 5y
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 climethestair 1663 — 5y
View all comments (2 more)
0
My bad xxXTimeXxx 101 — 5y
0
You're good, no worries. Thanks for the help! SnazzySpider67 53 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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)

0
Thanks a lot! SnazzySpider67 53 — 5y
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... SnazzySpider67 53 — 5y
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. SnazzySpider67 53 — 5y
Ad

Answer this question