How do I send a message from Server to Client

My current server code

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

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

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

Players.PlayerAdded:Connect(onPlayerAdded)

My current client 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 welcomeScreen = Instance.new("ScreenGui")
welcomeScreen.Parent = playerGui

local welcomeMessage = Instance.new("TextLabel")
welcomeMessage.Size = UDim2.new(0, 200, 0, 50)
welcomeMessage.Parent = welcomeScreen
welcomeMessage.Visible = false
welcomeMessage.Text = "Welcome to the game!"

local function onWelcomePlayerFired()
	welcomeMessage.Visible = true
	task.wait(3)
	welcomeMessage.Visible = false
end

welcomePlayerEvent.OnClientEvent:Connect(onWelcomePlayerFired)

I got this from roblox documentation, but Im getting Infinite yield possible on 'ReplicatedStorage:WaitForChild("WelcomePlayerEvent")'

youre probably firing before the client even reached the end, try adding a wait

local function onPlayerAdded(player)
player.CharacterAdded:Connect(function)
	welcomePlayerEvent:FireClient(player)
end)
end

Server Code:

game.Player.PlayerAdded:Connect(function(player)
	local WelcomePlayerAddedEvent = Instance.new("RemoteEvent")
	WelcomePlayerAddedEvent.Name = "WelcomePlayerAddedEvent"
	WelcomePlayerAddedEvent.Parent = game.ReplicatedStorage
	-- None of this needs to be done because you can already have the remote event in the replicated storage then just get the event name from here
	game.ReplicatedStorage.WelcomePlayerAddedEvent:FireClient(player)

	-- OR: Already have the event in the replicated storage so all you have to do is this:
	
	game.ReplicatedStorage.WelcomePlayerAddedEvent:FireClient(player)
end)

Client Code:

-- Note that using instance is not needed when you can already have the textlabel in a screengui
game.ReplicatedStorage.WelcomePlayerAddedEvent.OnClientEvent:Connect(function()
	local Message = Instance.new("ScreenGui")
	Message.Name = "Message"
	Message.Parent = game:GetService("Players").LocalPlayer.PlayerGui
	MessageText = Instance.new("TextLabel")
	MessageText.Name = "MessageText"
	MessageText.Parent = game:GetService("Players").LocalPlayer.PlayerGui.Message
	MessageText.Size = Udim2.new(0 ,200, 0, 50)
	MessageText.Position = --Set Pos
	MessageText.Visible = true
	wait(3)
	MessageText.Visible = false
end)

That should work but you don’t need to use Instance to make things, you should already have them placed. ex: the event in replicated storage and screengui and textlabel in the startergui

1 Like

Just a heads up: this may not work in studio because it loads both scripts and the character, so the event won’t have time to connect.