Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I send a variable to RemoteEvent to LocalScript?

I'd been making a game using a Timer to you know once the timer stops the game ends.

So I used 2 IntVariables, one called setSec (for setting the time in VIP Servers, assuming the server owner can set the time, public servers will set to 540), second one called sec (for counting down time, of course), and secremaining (the remaining of sec divide by 60)

Here are the scripts,

script in serverscriptstorage

local setsec = game.ServerStorage.setsec.Value
local second = game.ServerStorage.sec.Value
local min = math.floor(second/60)
local secremaining = second % 60
local secremainingval = game.ServerStorage.secremaining.Value
local remoteevent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
second = setsec
secremainingval = secremaining

for secremaining = secremaining, 0, -1 do
    remoteevent:FireAllClients(secremaining, min)
    secremaining = secremaining - 1
    wait(1)
end

local function send(secremaining, min)
    remoteevent:FireAllClients(secremaining, min)
end

for min = min, 0, -1 do
    remoteevent:FireAllClients(min)
    min = min - 1
    wait(1)
    wait(1)
end

LocalScript in timergui, which is in StarterGui

local remoteevent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local text = script.Parent.time

local function secremaining(secremaining, min)
    wait(1)
    if min > 9 then
        if secremaining - 1 < 10 then
            text.Text = "Time left: "..min..":0"..secremaining - 1
        else
            text.Text = "Time left: "..min..":"..secremaining - 1
        end
    else
        if secremaining - 1 < 10 then
            text.Text = "Time left: 0"..min..":0"..secremaining - 1
        else
            text.Text = "Time left: 0"..min..":"..secremaining - 1
        end
    end
end

remoteevent.OnClientEvent:Connect(secremaining)

local function setincorrecttime(secremaining, min)
    if secremaining == 0 then
        if min > 9 then
            text.Text = "Time left: "..(min-1)..":".."00"
        else
            text.Text = "Time left: 0"..(min-1)..":".."00"
        end
        min = min - 1
    end
end

remoteevent.OnClientEvent:Connect(setincorrecttime)

local function min(min)    
    if min ~= 0 then
        for i = 1, min do
            for i = 1, 60 do
                wait(1)
                if min > 9 then
                    if 60 - i < 10 then
                        text.Text = "Time left: "..min..":".."0"..(60-i)
                    else
                        text.Text = "Time left: "..min..":"..(60-i)
                    end
                else
                    if 60 - i < 10 then
                        text.Text = "Time left: 0"..min..":0"..(60-i)
                    else
                        text.Text = "Time left: 0"..min..":"..(60-i)
                    end
                end
            end
            min = min - 1
        end
    else
        for i = 1, 60 do
            wait(1)
            if min > 9 then
                if 60 - i < 10 then
                    text.Text = "Time left: "..min..":".."0"..(60-i)
                else
                    text.Text = "Time left: "..min..":"..(60-i)
                end
            else
                if 60 - i < 10 then
                    text.Text = "Time left: 0"..min..":0"..(60-i)
                else
                    text.Text = "Time left: 0"..min..":"..(60-i)
                end
            end
        end
    end
end

remoteevent.OnClientEvent:Connect(min)

And here is the error code from output:

18:29:05.230 - Players.CRTComputer.PlayerGui.timergui.localtimerchange:6: attempt to compare number and nil

So, the question is in the title. Thanks for answers if you have one.

(BTW, I'm a newbie.)

4 comments

0

Ignore my other answer unless it works.

0

it seems that min is nil. check if variable second is also nil (by printing it - print(second) in line3 in the script in the serverscriptstorage) if it is then the problem is with the game.ServerStorage.sec.Value

0

he has to reference the parameter

0

also when using thing like game.ReplicatedStorage or game.ServerStorage use local ReplicatedStorage = game:GetService("ReplicatedStorage") and local ServerStorage = game:GetService("ServerStorage") then type second = ServerStorage.sec.Value

1 answer

When you are calling the function, you have to make sure you have (parameters) after the function name. For example, if I do:

local function script(number)
	print(number)
end

I call it like this:

script(5)

Because we need to reference the parameters, which you didnt do, so changing line 21 to

remoteevent.OnClientEvent:Connect(secremaining(NUMBER FOR MIN))

And every other function call in your script to that should help.

Hope this helped.

~Dan_PanMan, advanced scripter.

This discussion is preserved as it was. New answers, comments, and votes are closed.