Sorry I can't explain this without it being too long but I really want you to understand the point.

So I have a module script that prints and returns a random string, or species in my case. The problem is I've had to heavily specify it in the script I reference it in to keep it from changing each time it's randomized.

Basically, it's like saying the variable X in a math equation can have a random chance of being equal to 1, 2, or 3 and X is different for each equation. But I want X to remain the same in ALL the equations. My module script example:

local possibleX = {}
local Players = game:GetService("Players")

local X = {'1', '2', '3'}

local variable = X[math.random(1, #X)]

local refrenceVariable

function possibleX.getClass()
	if variable == '1' then
		referenceVariable = '1'
	elseif variable == '2' then
		referenceVariable = '2'
	elseif variable == '3' then
		referenceVariable = '3'
	end
	return referenceVariable
end

return possibleX

So I've initialized x as being different in each equation. One script in ServerScriptService uses it:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local possibleX = require(ReplicatedStorage.MyFunctions)

local number = possibleX.getNumber()
_G.theNumber = 0

local function onCharacterAdded(character)
	if number == '1' then
		character.Head.Face.Texture = "rbxassetid://1"
		_G.theNumber = 1
		else if number == '2' then
			character.Head.Face.Texture = "rbxassetid://2"
			_G.theNumber = 2
			else if number == '3' then
				character.Head.Face.Texture = "rbxassetid://3"
				_G.theNumber = 3
			end
		end
	end
end

And another LocalScript in StarterGUI under a TextLabel uses it as well:

local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()

local textLabel = script.Parent

local content = "You've got nothing..."

repeat wait() until player.Character

if _G.number == 1 then
	content = "You got the number 1!"
	else if _G.number == 2 then
		content = "You got the number 2!"
		else if _G.number == 3 then
			content = "You got the number 3!"
		end
	end
end

-- Some content to spell out

for i = 1, #content do
	script.Parent.Text = string.sub(content,1,i)
	wait()
end

So tl;dr: I've tried many different was for the LocalScript of the TextLabel to find whether the variable in the server script is equal to a string, but I can't seem to figure out how to do so.

The main problem I'm having is that content isn't being changed when it goes through the IF method.

How can I fix this?

2 comments

0

You can't cross _G in through ServerScripts and LocalScripts. They only cross in same type. For example, only LocalScript can read _G from another LocalScript, a ServerScript cannot, and vice versa. https://devforum.roblox.com/t/what-is-g-and-for-what-i-can-use-it/322199

0

Oops, I guess I had the idea of a global variable wrong. Thank you!