LocalScript isn't detecting remote event FireAllClients()

With this setup in mind:

pict

-- Script in ServerScriptService

local RS = game:GetService("ReplicatedStorage")
local event = RS:WaitForChild("RemoteEvent")

event:FireAllClients(some_data)
-- LocalScript in StarterPlayerScripts

local RS = game:GetService("ReplicatedStorage")
local event = RS:WaitForChild("RemoteEvent")

local PLYRS = game:GetService("Players")
local plyr = PLYRS.LocalPlayer

local function main(some_data)
    while not plyr and not plyr.PlayerGui and not plyr.PlayerGui:FindFirstChild("ScreenGui") do
        wait()
    end
    
    plyr.PlayerGui.ScreenGui.TextBox,Text = some_data
end

event.OnClientEvent:Connect(main)

Hope this helps.

Hey, sorry I took so long to respond, but for the some_data part, what would I put if I wanted to send the string of text “10” through and then use it in the other script?

For this, use shared or _G. Both are global tables that can be accessed locally, or on the server, between scripts.

To send data through a RemoteEvent:

local function main(some_data)
    shared.some_key = some_data
    print(shared.some_key)

    -- or...

    _G.some_key = some_data
    print(_G.some_key)
end

event.OnClientEvent:Connect(main)

Hope this helps.

This all works and the LocalScript is detecting the RemoteEvent. The issue was I think that the RemoteEvent was being fired before all the scripts had loaded fully. But now I’m getting an error message saying that ScreenGui is not a valid member of the PlayerGui

Is there a way I can do a WaitForChild() thing with the PlayerGui?

while not your_instance do
    your_instance = path_to_your_instance
    task.wait()
end

What’s your use-case for using a RemoteEvent here? It’s generally better practice to not use _G or shared; perhaps you’re asking the wrong questions?

I’m using the RemoteEvent to activate an action that should affect all players through the server because the issue I was originally having was that when a player died, it would reset the LocalScript that was changing a GUI back to the top of the script when it shouldn’t do that.

There’s a property belonging to the ScreenGui class called ResetOnSpawn, which will prevent the GUI being destroyed.

What do you mean here?

Oh it was actually that simple? I had no idea. Let me revert things back to how I had them originally and see if that toggle fixes it

1 Like

If I made all the changes are through a LocalScript and not serverside, wouldn’t that mean that depending on when you joined the game, the GUI would be different?

To elaborate, you join the game and the GUI script starts up on:
“Intermission: 30”
Then it keeps counting down. But what if someone joins 5 seconds later?
Wouldn’t their GUI be at “Intermission: 30” and the the other player’s at “Intermission: 25”?

I see your use-case. The elaboration helps.

In which case, you should be handling very little on the client.

Try this approach:

-- Script in ServerScriptService

local count = 30

-- Count on the server, this means there isn't inconsistency between clients!
while true do
    count -= 1
    event:FireAllClients(count)
    task.wait(1)
end
-- LocalScript in StarterPlayerScripts

local function main(count)
    gui.TextLabel.Text = tostring(count)
end

event.OnClientEvent:Connect(main)

LocalScripts affect individual clients. Server scripts affect everything owned by the server.

Hope this helps, please let me know if anything doesn’t make sense.

This works, thank you very much for helping me! All I need to do now is to fiddle around with the ServerScript so once count reaches zero, it transfers to a different message

1 Like

When I finish with the modifications, I’ll post the code so if anyone wants to use it, they can

Here is the finished code:

–ServerScriptService Script

wait(.1)

while true do

local RS = game:GetService(“ReplicatedStorage”)

local event = RS:WaitForChild(“FirstEvent”)

local eventTwo = RS:WaitForChild(“SecondEvent”)

local eventThree = RS:WaitForChild(“ThirdEvent”)

local count = 30

while count > 0 do

count -= 1

event:FireAllClients(count)

task.wait(1)

end

count = 11

while count > 0 do

count -= 1

eventTwo:FireAllClients(count)

task.wait(1)

end

eventThree:FireAllClients()

wait(37.8) --This number is so specific because it’s how long until the lava in my game disappears

end

–LocalSciprt in StarterPlayerScripts

local RS = game:GetService(“ReplicatedStorage”)

local event = RS:WaitForChild(“FirstEvent”)

local eventTwo = RS:WaitForChild(“SecondEvent”)

local eventThree = RS:WaitForChild(“ThirdEvent”)

local PLYRS = game:GetService(“Players”)

local plyr = PLYRS.LocalPlayer

local function Intermission(count)

plyr.PlayerGui.ScreenGui.TextBox.Text = "Intermission: "…tostring(count)

end

local function RisingCountDown(count)

plyr.PlayerGui.ScreenGui.TextBox.Text = "Lava Is Rising In "…tostring(count)

end

local function Warning()

plyr.PlayerGui.ScreenGui.TextBox.Text = “The Lava is Rising! Seek High Ground!”

end

event.OnClientEvent:Connect(Intermission)

eventTwo.OnClientEvent:Connect(RisingCountDown)

eventThree.OnClientEvent:Connect(Warning)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.