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

how to share variables between scripts?

Hello Everyone!

I needed to use a variable I declared in a script in another script, but I didn't know how, some

some people said use _G but it doesn't seem to work for me, can you guys tell me how to do it?

Script 1:

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
_G.TheClone = game.ServerStorage.Invisibility:clone()

local gamePassID = 20730613 --Replace this set of numbers with your gamepass id

function onPlayerSpawned(player) 

	local hasPass = false

	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.userId, gamePassID)
	end)
	-- The script below is for checking whether or not the player has the gamepass.
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end

	if hasPass == true and player.Team == Teams.Runners then
		TheClone.Parent = player.Backpack i
	end
end 

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function()
		onPlayerSpawned(player)
	end)
end)

Players.PlayerSpawned:Connect(onPlayerSpawned)

Script 2:

local tool = script.Parent
local Players = game:GetService("Players")
local localplayer = Players.LocalPlayer
char = _G.TheClone.Parent

tool.Equipped:Connect(function()
	tool.Activated:Connect(function()
		local function setTransparency(char, value)
			for _, child in pairs(char:GetChildren()) do
				if child:IsA('Hat') and child:FindFirstChild("Handle") then
					child = child.Handle
				elseif child:IsA('BasePart') then
					child.Transparency = value
				end
			end
		end

		local enabled = true
		
		local char = _G.TheClone.Parent
			if char then
				local head = char:FindFirstChild("Head")
				local face = head:FindFirstChild("face")
			if enabled and head and game.Players:GetPlayerFromCharacter(char) then
					enabled = false
					for t = 0, 1, .1 do
						if face then face.Transparency = t end
						setTransparency(char, t)
						wait(0.1)
					end
					wait(10)
					for t = 1, 0, -.1 do
						if face then face.Transparency = t end
						setTransparency(char, t)
						wait(0.1)
					end
					wait(2)
					enabled = true
				end
			end)
	end)
end)

3 comments

0

plz help I am a beginner

0

_G can't pass the client / server boundary. In other words, the client has its own _G table and the server has its own _G table.

0

gonna try that

1 answer

_G most likely didnt work because your trying to pass something from server to client or client to server

if i would like to pass a var from the client to server or server to client i would want a remoteEvent

remoteEvent works like this

--Client
local remoteEvent = game.ReplicatedStorage.RemoteEvent

Example
remoteEvent:FireServer(script.Parent)
--Server
local remoteEvent = game.ReplicatedStorage.RemoteEvent

--REMEMBER THE FIRST VAR HERE IS ALWAYS THE PLAYER
--THE SECOND VAR IS THE FIRST VAR YOU PASSED ON THE CLIENT AND SO ON
remoteEvent.OnServerEvent:Connect(function(Player, Parent)
	--if Server gets the remoteEvent signal then it will fire and run the code down here
end)

1 comment

0

gonna try that

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