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

How do I fire a remote event? (Server script to local script / Server to client)

Asked by
uhjos_h 19
3 years ago

I am trying to fire a RemoteEvent, and this is my first time ever using RemoteEvents. I am currently looking on different sites for answers, like developer.roblox.com, where they have a section for RemoteEvents, so I took code samples from there, and edited them.

Server Sided Code

local player = game.Players.LocalPlayer




local Owners = {"SkyFume"}

local Superadmins = {}

local Admins = {}

local Mods = {}

local CustomRanks = {}

game.Players.PlayerAdded:Connect(function(Player)
    for i, v in pairs(Owners) do
        if Player.Name == v then
            local Players = game:GetService("Players")

            local welcomePlayerEvent = Instance.new("RemoteEvent")
            welcomePlayerEvent.Parent = game.ReplicatedStorage
            welcomePlayerEvent.Name = "WelcomePlayerEvent"

            local function onPlayerAdded(player)
                welcomePlayerEvent:FireClient(player)
            end

            Players.PlayerAdded:Connect(onPlayerAdded)
        end
    end
end)

Local Script Code

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local welcomePlayerEvent = ReplicatedStorage:WaitForChild("WelcomePlayerEvent")
local playerGui = player:WaitForChild("PlayerGui")
local ScreenGui = script.Parent.ScreenGui


local function onWelcomePlayerFired()
    ScreenGui.Parent = playerGui
end



welcomePlayerEvent.OnClientEvent:Connect(onWelcomePlayerFired)

1 answer

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

You are running the RemoteEvent in a wrap of two PlayerAdded events? It will absolutely not work because PlayerAdded event will never fire in PlayerAdded signal. It is surely complicated but it is redundant to do so, and it will not work.

You can consider putting the FireClient event outside, since you already have a player parameter in the first PlayerAdded event, you can just fire it directly with the parameter.

Also, server does not have Players.LocalPlayer. You are going to get it using PlayerAdded and get the parameter. Where you already have it so you can remove the line.

Going to the LocalScript, I've noticed a problem is that if you are not in the Owner table, you are going to yield the WaitForChild from the ReplicatedStorage, and never get a respond and return a warning.

I'd recommend you put RemoteEvent inside ReplicatedStorage first. That will be more simpler.

Last problem, and this is from the LocalScript. You never assigned a parameter for player in the OnClientEvent function. Which will make an error "Arguments 1 missing or nil". To fix this, you need to assign the player parameter into the function. Also another problem is you are trying to clone ScreenGui into LocalPlayer? I believe you meant making the GUI clones to the player who have ran the RemoteEvent.

Server Script:

local Owners = {"SkyFume"}

local Superadmins = {}

local Admins = {}

local Mods = {}

local CustomRanks = {}

game.Players.PlayerAdded:Connect(function(Player)
    for i, v in pairs(Owners) do
        if Player.Name == v then
            -- local Players = game:GetService("Players") This is redundant. It is located but never used. You can remove this if you want.
            local welcomePlayerEvent = game:GetService("ReplicatedStorage").welcomePlayerEvent

        welcomePlayerEvent:FireClient(Player)
        end
    end
end)

Client Script:

local welcomePlayerEvent = ReplicatedStorage.welcomePlayerEvent

local function onWelcomePlayerFired()
    local Players = game:GetService("Players")
    local ReplicatedStorage = game:GetService("ReplicatedStorage")

    local player = Players.LocalPlayer
    local playerGui = player:WaitForChild("PlayerGui")
    local ScreenGui = script.Parent.ScreenGui

    ScreenGui.Parent = playerGui
end

welcomePlayerEvent.OnClientEvent:Connect(onWelcomePlayerFired)
0
This answer is the longest I had ever post (I think) Xapelize 2658 — 3y
0
This still does not work, I did some digging- and I still found nothing? My output doesn't seem to show errors, but it seems that it fires the event, but the LocalScript doesnt receive it uhjos_h 19 — 3y
0
Is your LocalScript located on StarterPlayer > StarterPlayerScripts? Since LocalScript does not run in Workspace or ServerScriptService. Xapelize 2658 — 3y
0
Players.SkyFume.PlayerScripts.LocalScript:1: attempt to index nil with 'welcomePlayerEvent'. Any idea? uhjos_h 19 — 3y
View all comments (2 more)
0
Did you read the big text on my answer? You should put a RemoteEvent called "welcomePlayerEvent" in ReplicatedStorage. I think that's the problem. Xapelize 2658 — 3y
0
There is a RemoteEvent in ReplicatedStorage, it is called welcomePlayerEvent. uhjos_h 19 — 3y
Ad

Answer this question