Ok. So I want to make a fighting arena where 2 players can click a button and be assigned player one or player two. To get who clicked the button I used this local script:
wait(.1) --Wait for player to load in player = game.Players.LocalPlayer --Get the local player _G.player1 = nil --Declare player1 global variable _G.player2 = nil --Declare player2 global variable script.Parent.TextButton.MouseButton1Click:connect(function() --Runs function if player clicks the button if _G.player1 == nil then --Make sure player1 has not been chosen yet _G.player1 = player --If it hasnt been chosen, assign local player to player1 elseif _G.player1 ~= player then --Makes sure the same player has not clicked it twice _G.player2 = player --If it hasnt been chosen, assign local player to player2 game.Workspace.PlayerChosen:FireServer() --Fire the spawn event in my normal script that you will see later end end)
My normal script:
local event = Instance.new("RemoteEvent") --makes the spawn event event.Name = "PlayerChosen" --Names the event event.Parent = game.Workspace --placing the event in workspace event.OnServerEvent:connect(function() --runs when event is called in local script print("Spawning " .. _G.player1 .. " and " .. _G.player2 .. ". Good luck!") --Just used to see if the event did run _G.player1 = nil --sets player1 and 2 back to nil so they can be chosen again _G.player2 = nil end)
So you are probably asking what the problem is. Well when I click the button, nothing happens. The button is set up as a text button and is adorneed to a brick. No errors. No output that is in the normal script. No nothing.
Variables stored in _G
are global to the main thread. Every time you have a new script running in your game, it's treated as a thread that is handled by ROBLOX's thread scheduler. Think of it as multiple functions within a script, all being executed by that script. The "script" in that situation, would be where _G
is held, for all the functions in the script to have access to. Here's a demonstration:
local _G = {} -- Execute functions in order, where each function represents a script in your game. -- Script 1 local function script1() _G.test = "Hello" -- Store test:"Hello" in _G end -- Script 2 local function script2() print(_G.test) -- > "Hello" end -- This assumes script 1 is running before script 2 without yield. script1() script2()
I'm not saying this is exactly how it works behind the scenes, but it's the same idea in terms of how _G
is accessed.
Anyway, the server
and the client
are two different physical machines (your personal computer being the client, and the machine hosting the game being the server). Needless to say, both machines aren't going to be sharing resources with each other during runtime. They each have their own running environment, with their own main thread.
To put it simply: _G
in a local script is only going to remain global to other local scripts ran by your computer, and _G
in a server script will only remain global to other server scripts ran by the server. If you'd like communication between client and server, just use remotes like you have been already.
Hope this helped, if you have any questions, just let me know.