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

Why won't RemoteEvent work?

Asked by
attapi0 20
9 years ago

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.

0
This:- "gameStart.FireAllClients()" should be this "gameStart:FireAllClients()" User#5423 17 — 9y
0
Hm, that didn't seem to change anything. Thanks for the response though. attapi0 20 — 9y
0
I just saw this but you dident set the parent of the remote event? User#5423 17 — 9y
0
I followed your advice and parented the RemoteEvent gameStart to the script.Parent. This had no effect. Thanks again. attapi0 20 — 9y
View all comments (2 more)
0
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? M39a9am3R 3210 — 9y
0
I don't quite follow. Can you elaborate in a full answer? I'm extremely new to how Roblox does this kind of stuff. attapi0 20 — 9y

2 answers

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

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.

Functionality of Events and Scripts

Server Sided Script are typically designed to work in Workspaceor ServerScriptService.

RemoteEvents and RemoteFunctions are designed to work in any location that clients have access to such as Workspace, or ReplicatedStorageare 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).

Fixing the Script

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)

Conclusion

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.

0
This worked perfectly and answered all of my questions. Thanks so much, I owe you one. attapi0 20 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Scripts do not execute in ReplicatedStorage. Move the server script to the ServerScriptService.

0
Still no luck :/ attapi0 20 — 9y

Answer this question