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

Referencing a variable passed from server to client?

Asked by 5 years ago

Hi

I'm making a very simple race against the clock game. The player has to run a very simple course, and they see their time updated every second. There is a part on the finish line which, when they touch, can tell them their finish time. Each second I'd like to send the time from the server to the client. I will then post this updated time in a screengui on the client side.

I have a server script which fires to the client when the player is added, and I would like it to fire a start time to the client. The bit I am struggling with is how to refer to the start time variable on the client side. The server code is held in "ServerScriptService" and the client code is held in "StarterGui".

In the client side I am getting a warning when I refer to the server side "starttime" variable, and I am getting an error which says "Argument 1 - missing ot nil"

Thank you once again for your help.

Chris

--- Server side script---

local Players = game:GetService("Players")
local welcomePlayerEvent = Instance.new("RemoteEvent")
welcomePlayerEvent.Parent = game.ReplicatedStorage
welcomePlayerEvent.Name = "WelcomePlayerEvent"
local starttime=0  -- this is the start time variable

local function onPlayerAdded(player)
-- This is where I think I am sending the starttime to the client
    welcomePlayerEvent:FireClient(player,starttime)
end

Players.PlayerAdded:Connect(onPlayerAdded)

--- Client Side Script

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local welcomePlayerEvent = ReplicatedStorage:WaitForChild("WelcomePlayerEvent")
local playerGui = player:WaitForChild("PlayerGui")

-- *** this line is where I am getting a warning "Unknown Global starttime"
local time = ReplicatedStorage:WaitForChild(starttime)


-- Create a screengui which posts a welcome message to a player

local welcomeScreen = Instance.new("ScreenGui")
welcomeScreen.Parent = playerGui
local welcomeMessage = Instance.new("TextLabel")
welcomeMessage.Size = UDim2.new(0, 200, 0, 50)
welcomeMessage.Parent = welcomeScreen
welcomeMessage.Visible = false
welcomeMessage.Text = "Welcome to the game!"..player.name

local function onWelcomePlayerFired()


    welcomeMessage.Visible = true

    for i=1,10,1 do
       wait(1)
    end
    welcomeMessage.Visible = false
end

welcomePlayerEvent.OnClientEvent:Connect(onWelcomePlayerFired)

0
Why don't you just run the clock client side and server side at the same time and when the player reaches the end, it will compare the two clocks and then display the server clock if the client clock doesn't match, else it will just display the client clock (less data being sent this way). SteamG00B 1633 — 5y
0
did you mean at line 9 "starttime" and not starttime? maumaumaumaumaumua 628 — 5y
0
in client side script maumaumaumaumaumua 628 — 5y
0
SteamG0D : thank you. How do I compare them the client and server clock? Presumably I need to pass the server time to the client, or vice-versa, and that is where I've got stuck. redchris999 4 — 5y
View all comments (2 more)
0
You indeed have to send one of the values to the other end. Shouldn't truly matter which way but I always prefer to send the data from the client to the server. At least that way you for sure know it's properly validated as the client could screw with it. As for comparing them, I'd simply use 2 unix timestamps. User#834 0 — 5y
0
Thank you Flowery. I think my question is much more basic. In line 9 I am trying to read the server variable into the client . However, no matter how many different ways I try, I can't find the correct syntax for doing this. So my question is "what is the correct syntax". I know my current line 9 is wrong, but I'd like to know how to write it correctly. redchris999 4 — 5y

Answer this question