Using global variables between LocalScripts and Normal/ServerScripts

I’m trying to make a cart script for purchasing items, using FireClient and OnClientEvent.
Just a quick question, How would you be able to use variables across multiple scripts, so it can be used for a function on a different script?
I tried using _G. but i found out global variables used on ServerScripts can only be used on the same type, and can’t be used on LocalScripts.

ServerScript

--To call values 
local GoToCart = game:GetService("ReplicatedStorage"):WaitForChild("ItemToCart")
local InsertService = game:GetService("InsertService")
local ClickDetect = script.Parent.ClickDetector
Manniquin = script.Parent
_G.ShirtNum = Manniquin.ShirtID.Value
_G.PantNum = Manniquin.PantID.Value
_G.Pants = Manniquin.Pants
_G.Shirts = Manniquin.Shirt
_G.PantAsset = InsertService:LoadAsset(_G.PantNum)
_G.ShirtAsset = InsertService:LoadAsset(_G.ShirtNum)
-- Change Items to NumberValues
if _G.ShirtNum or _G.PantNum > 0 then
	_G.Pants.PantsTemplate ="http://www.roblox.com/asset/?id=".. _G.PantAsset
	_G.Shirts.ShirtTemplate = "http://www.roblox.com/asset/?id=".. _G.ShirtAsset
	wait(0.2)	
end

--For Adding Items to the Cart 
function AddItem()
	GoToCart:FireClient()
end

ClickDetect.MouseClick:Connect(AddItem)

and the Localscript

waitHuh = game:GetService("ReplicatedStorage"):WaitForChild("ItemToCart")
Items = script.Parent
function Poro(values,tryshr,valuep,tryp)
	if values ~= "" then
		if Items.Item1.Image == "" then
			Items.Item1.Image = tostring("http://www.roblox.com/Thumbs/Asset.ashx?format=png&width=100&height=100&assetId=" .. values)
			Items.Item1.ClothingID = values
			Items.Item1.tryvalue = tryshr
			Items.Item1.Type = "Shirt"
end
end
waitHuh.OnClientEvent:Connect(Poro)

I’m Confused.

1 Like

Instead of using _G, you can use values that you put in a folder somewhere in replicated storage. It should be a good alternative.

2 Likes

You can put them in a ModuleScript in ReplicatedStorage, and require it from both the Script and LocalScript.

Yours would look something like this:

local module = {
	ShirtNum = Manniquin.ShirtID.Value,
	PantNum = Manniquin.PantID.Value,
	-- and so on
}

return module
2 Likes

Could it also be possible to pass the values with arguments?

1 Like

What do you mean by arguments?

1 Like

I think they mean arguments like this:

local function example(argument)

end
2 Likes

Pretty much what RipPBB said. instead of going from a script to module to script, I think it would be possible to do it like:

ClickDetect.MouseClick:Connect(function(player)
		local values = ShirtNum
		local tryshr = ShirtAsset
		local valuep = PantNum
		local tryp = PantAsset
		wait(0.5)
		GoToCart:FireClient(player, values, tryshr, valuep, tryp)
	end)

but im not sure if it will work correctly.

1 Like

I’m not sure I understand what you mean by that, but that code would work.

2 Likes

Yea, it worked. I modified the ServerScript a little bit. I noticed that the client wasn’t receiving the arguments/parameters since a few of the values were not even loaded in correctly. :joy:

local PantAsset = InsertService:LoadAsset(PantNum)
local ShirtAsset = InsertService:LoadAsset(ShirtNum)
local ShirtRaw = ShirtAsset.Pants.PantsTemplate
local PantRaw = PantAsset.Pants.PantsTemplate
local ShirtID = ShirtRaw:match("%d+")
local PantID = PantRaw:match("%d+")
-- Change Items to NumberValues
if ShirtNum or PantNum > 0 then
	print(ShirtID)
	Pclothing.PantsTemplate = "rbxassetid:/".. PantID
	sclothing.ShirtTemplate = "rbxassetid:/".. ShirtID
	wait(0.2)	
end

--For Adding Items to the Cart 

ClickDetect.MouseClick:Connect(function(player)
		local values = ShirtNum
		local tryshr = ShirtID
		local valuep = PantNum
		local tryp = PantID
		wait(0.5)
		GoToCart:FireClient(player, values, tryshr, valuep, tryp)
	end)

It looks almost the same but I added a few more variables to separate the ID from the actual asset URL. Though using modules would have worked regardless.

2 Likes