Passing a variable into a local script with a remote event

I have a sort of Natural Disaster Game or whatever and I want to pass a variable which holds a random item of a table to a local script.

Server Script:

local serverstorage = game:GetService(“ServerStorage”)
local DisastersFolder = serverstorage:WaitForChild(“Disasters”)
local Disasters = DisastersFolder:GetChildren()
local button = script.Parent
local DisastersInProgress = workspace.Map.DisastersInProgress
local clickdec = script.Parent.ClickDetector
local db = false

clickdec.MouseClick:Connect(function(plr)
if db == false then
local count = #Disasters
db = true
local choiceindex = math.random(1, count)
local choice = Disasters[choiceindex]
print(choice)
game:GetService(“ReplicatedStorage”).DisasterGUI:FireAllClients(choice)
task.wait(5)
choice.Parent = DisastersInProgress
task.wait(10)
choice.Parent = serverstorage
db = false
end
end)

Local Script:

local repstorage = game:GetService(“ReplicatedStorage”)
local StarterGui = game:GetService(“StarterGui”)
local Text = StarterGui.TextGui.DisasterText
local DisasterGui = repstorage.DisasterGUI

DisasterGui.OnClientEvent:Connect(function(choice)
print(choice)
Text.Text = “Chosen Disaster: “…choice…”.”
end)

When I print “choice” on the client script it prints nil. I don’t know why. Anything solution I appreciate

is choice printing nil on the server or client?

Client, sorry for the confusion

Does the text change to the choice?

It errors:
Players.MaybeTheCake.PlayerGui.TextGui.DisasterText.LocalScript:8: attempt to concatenate nil with string

you’re sending a number to the client

it cant set text to a number unless you use tostring(choice)

yes, but i need the variable “count” in the server script to pass into the local script, but it is nil

if you need ‘count’ on the local script, why are you sending choice

sry i meant choice, sorry if this is frustrating you

you’re sending a number as ‘choice’ so it can’t be set to the text unless you either convert it to text based on the number in the server script, or in the local script and set the text to that.

server script example:

if choice == 1 then
local c = "Tornado"
elseif choice == 2 then
local c = "Tsunami"

game:GetService(“ReplicatedStorage”).DisasterGUI:FireAllClients(c)

when i print choice it prints “Laser” and laser is the only disaster in the folder as im doing the scripting first.
image
so i dont know why you are saying its a number?
also i made the concatenation to, tostring

is it printing “Laser” from the server?

uhhhhhhhhhhhh guys wait i think i have solution but probably the most unlikliest; put wait statement on client bcz i think for the client the disasters havent loaded yet so it appears as nil due to the fact Disasters[choiceindex] = nil on the clientside

or server it depends

message limit message lgjk fsdjkbdnjd

yes it is printing laser from the server

will someone check out my solution or no

1 Like

but it’s not printing from the client, so it’s not sending “Laser” to the client, it’s sending the number from math.random(1, count)

i already tried it, it doesnt work

did you put wait or task.wait on client or server? the event might be fired to soon for the cleint to load so thats why

so then how would i make it so it sends the disaster name