This is a pretty straightforward problem. Here's my server script (Located in ServerScriptStorage).
GameInProgress = false if GameInProgress == false then GameInProgress = true wait(1) gameStart = Instance.new("RemoteEvent") gameStart.Parent = script.Parent gameStart.Name = "GameStarting" gameStart:FireAllClients() end
Here is my client-side script, located in StarterGui.
function drawGameStart() local screenGui = Instance.new("ScreenGui") screenGui.Parent = script.Parent local textLabel = Instance.new("TextLabel") textLabel.Visible = true textLabel.Parent = screenGui textLabel.Position = UDim2.new(0.5, 0, 0.5, 0) textLabel.Size = UDim2.new(0, 0, 0, 0) textLabel.BackgroundTransparency = 1 textLabel.BorderSizePixel = 0 textLabel.FontSize = Enum.FontSize.Size48 textLabel.Text = "A new game is starting..." textLabel.Font = "SourceSans" textLabel.TextColor3 = Color3.new(255, 255, 255) textLabel.TextStrokeTransparency = 0 wait(10) textLabel.Visible = false end local event = script.Parent:WaitForChild("GameStarting") event.OnClientEvent:connect(function() drawGameStart() end)
I'm not getting any errors. The function drawGameStart() is supposed to draw a textLabel in the center of the screen that says "A new game is starting..." It works on its own, but when I try to call it with a RemoteEvent, it doesn't work.
Please tell me if I'm making any brain-dead mistakes.
Edit: updated code to what I have now.
Edit2: Something is definitely wrong on my end. I literally just copy and pasted the examples from the wiki and even those don't work.
Alright, since it has been requested. I will elaborate further what I mean by my own question to the Asker.
Why not have ServerScript placed in ServerScriptService, have remote event placed in ReplicatedStorage, and have the client side script access the RemoteEvent and communicate to the server?
What I meant by this, why not use a clean way to store scripts and other objects than to have them be in locations they may not necessarily work.
Server Sided Script are typically designed to work in Workspace
or ServerScriptService
.
RemoteEvents and RemoteFunctions are designed to work in any location that clients have access to such as Workspace
, or ReplicatedStorage
are recommended.
Finally, LocalScripts can only run with the client, meaning they must be in the player's Backpack
, PlayerGui
, or Character
(I am unsure of how they act simply in the player and have not experimented with them in this location).
Place this code in a Server Side Script in ServerScriptService
--First off, I recommend setting up the RemoteEvents first that way you can still reference it throughout the script. GameInProgress = false local gameStart = Instance.new("RemoteEvent", game.ReplicatedStorage) --'game.ReplicatedStorage' will automatically place the event in ReplicatedStorage, if the RemoteEvent is located in ServerScriptService, then there is no way for a client script to connect to it. You also will not need the gameStart.Parent in the script later on. gameStart.Name = "GameStarting" if GameInProgress == false then --I prefer using 'not' when using an argument like this, but this should work too. GameInProgress = true wait(1) gameStart:FireAllClients() --I see this will send a signal to all clients. --GameInProgress is currently true and at this point it looks like no reversing it. end
After reading your code above a bit, it does appear that there is a chance where the event will fire to all clients before the client has the chance to even load its character. To fix this possibly is by inserting the line;
repeat wait() until game.Players.NumPlayers >= 1 --This will cause the script to keep waiting 1/30 of a second until the number of players in the Players Service is greater than or equal to 1.
As for your client script, it looks fine. I will just make some personal preference tweaks. This script should be located in StarterGui, this way it will be replicated to the Clients under PlayerGui and run.
function drawGameStart() local screenGui = Instance.new("ScreenGui") screenGui.Parent = script.Parent local textLabel = Instance.new("TextLabel") textLabel.Visible = true textLabel.Parent = screenGui textLabel.Position = UDim2.new(0.5, 0, 0.5, 0) textLabel.Size = UDim2.new(0, 0, 0, 0) textLabel.BackgroundTransparency = 1 textLabel.BorderSizePixel = 0 textLabel.FontSize = Enum.FontSize.Size48 textLabel.Text = "A new game is starting..." textLabel.Font = "SourceSans" textLabel.TextColor3 = Color3.new(255, 255, 255) textLabel.TextStrokeTransparency = 0 wait(10) --textLabel.Visible = false --If you are not going to use the text label anymore anyways, then why not just delete it? You can use the destroy method to remove the part. textLabel:Destroy() end repeat wait() until game.ReplicatedStorage:FindFirstChild("GameStarting") --I prefer to use this method, mainly because I am not used to WaitForChild. This portion of the script will continue waiting until GameStarting exists. game.ReplicatedStorage.GameStarting.OnClientEvent:connect(function() drawGameStart() end)
I hope my answer explained some things. Such as the Client must have access to RemoteEvents or RemoteFunctions in order to work. If you have any questions, feel free to leave a reply. If this helped then please upvote it. Once again, I have mentioned this, it is entirely possible the event is fired before the Character has a chance to spawn so, you can use other methods to find if the player's character has loaded.
Scripts do not execute in ReplicatedStorage. Move the server script to the ServerScriptService.