_G variable printing 'nil'

Hi it prints ‘nil’. I want to send a value that works in localscript to other script. If there is a better method, feel free to let me know.

Localscript:

task.wait(3)
	
_G.test = game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.TextBox.Text

Script:

task.wait(6)

print(_G.test)
1 Like

You want to send it from local script to basic script?

Yes, sometimes some codes can only work in localscript, but for example, when I send an https request, I need a server script.

You can not get globals from client → sever and vise versa. Only client → client and server → server.

3 Likes

Changes like these made on the client will not replicate to the server (although there are some exceptions, none apply to your case). You would need to use a RemoteEvent to achieve this.

1 Like

Use Remote Events

If you want short example:
Put Remote Event into ReplicatedStorage and give it name ChangePlayerTextBoxText
LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("ChangePlayerTextBoxText")
local Test = game.Players.LocalPlayer.ScreenGui.Frame.TextBox.Text
RemoteEvent:FireServer(Test)--this will fire RemoteEvent on server with think called Test like you will be able to use it in basic script--

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("ChangePlayerTextBoxText")
RemoteEvent.OnServerFired:Connect(function(player,Test) -- Remote Event must first value must be always to be player you can change it name how you want it just the player who fired event, next is think with which you fired event --
       print(Test)
end)

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