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

[SOLVED] How can I make server globals work on the client?

I have a tool, but to use the mouse function, it needs to be a local script. If I make it a local script, the global functions won't work. I can't think of a quick way to resolve this issue. The tool's script is:

localPlr = game.Players.LocalPlayer
gui = nil

function displayClass(plr,class)
	if gui ~= nil then
		gui:Destroy()
	end
	gui = script.Parent.ClassGUI:Clone()
	gui.Background.Player.Text = plr.Name..":"
	gui.Background.Class.Text = class
	gui.Parent = localPlr.PlayerGui
end

script.Parent.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
		local target = mouse.Target
		if target == nil then
			if gui ~= nil then
				gui:Destroy()
			end
			return
		end
		local plr
		if target.Parent:IsA("Model") and game.Players:FindFirstChild(target.Parent.Name) ~= nil and game.Players[target.Parent.Name]:IsA("Player") then
			plr = game.Players[target.Parent.Name]
		elseif target.Parent.Parent:IsA("Model") and game.Players:FindFirstChild(target.Parent.Parent.Name) ~= nil and game.Players[target.Parent.Parent.Name]:IsA("Player") then
			plr = game.Players[target.Parent.Parent.Name]
		elseif gui ~= nil or plr == nil then
			gui:Destroy()
			return
		end
		if _G.checkAsset(plr,"Supreme Investor") then
			displayClass(plr,"Supreme Investor")
		elseif _G.checkAsset(plr,"Investor") then
			displayClass(plr,"Investor")
		elseif _G.checkAsset(plr,"FirstClass") then
			displayClass(plr,"First Class")
		elseif _G.checkAsset(plr,"BusinessClass") then
			displayClass(plr,"Business Class")
		else
			displayClass(plr,"Economy")
		end
	end)
end)

script.Parent.Unequipped:connect(function()
	if gui ~= nil then
		gui:Destroy()
	end
end)

1 comment

0

What do you mean by global functions? Do you need to use module scripts?

2 answers

✓ Accepted answer

Very rarely is _G the best way to solve a problem.

You should use ModuleScripts in order to share functions between many scripts.

If _G.checkAsset is implemented using code that can be run on either the client or the server, you don't have to do any work -- both LocalScripts and Scripts just require the same module.

Otherwise, you can make a module for the Client that abstracts away the difficulties of RemoteFunctions, and do the work on the server as needed.

make a folder in the workspace called "GlobalVars"

put in value objects in the folder and get them by doing this.

this is to get a value called MyString

myString = workspace.GlobalVars.MyString.Value

2 comments

1

If you want to do that, use ReplicatedStorage instead of workspace.

0

Would work for global variables, but not global functions.

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