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

01local player = game.Players.LocalPlayer
02 
03 
04 
05 
06local Owners = {"SkyFume"}
07 
08local Superadmins = {}
09 
10local Admins = {}
11 
12local Mods = {}
13 
14local CustomRanks = {}
15 
View all 32 lines...

Local Script Code

01local Players = game:GetService("Players")
02local ReplicatedStorage = game:GetService("ReplicatedStorage")
03 
04local player = Players.LocalPlayer
05local welcomePlayerEvent = ReplicatedStorage:WaitForChild("WelcomePlayerEvent")
06local playerGui = player:WaitForChild("PlayerGui")
07local ScreenGui = script.Parent.ScreenGui
08 
09 
10local function onWelcomePlayerFired()
11    ScreenGui.Parent = playerGui
12end
13 
14 
15 
16welcomePlayerEvent.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:

01local Owners = {"SkyFume"}
02 
03local Superadmins = {}
04 
05local Admins = {}
06 
07local Mods = {}
08 
09local CustomRanks = {}
10 
11game.Players.PlayerAdded:Connect(function(Player)
12    for i, v in pairs(Owners) do
13        if Player.Name == v then
14            -- local Players = game:GetService("Players") This is redundant. It is located but never used. You can remove this if you want.
15            local welcomePlayerEvent = game:GetService("ReplicatedStorage").welcomePlayerEvent
16 
17        welcomePlayerEvent:FireClient(Player)
18        end
19    end
20end)

Client Script:

01local welcomePlayerEvent = ReplicatedStorage.welcomePlayerEvent
02 
03local function onWelcomePlayerFired()
04    local Players = game:GetService("Players")
05    local ReplicatedStorage = game:GetService("ReplicatedStorage")
06 
07    local player = Players.LocalPlayer
08    local playerGui = player:WaitForChild("PlayerGui")
09    local ScreenGui = script.Parent.ScreenGui
10 
11    ScreenGui.Parent = playerGui
12end
13 
14welcomePlayerEvent.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