Randomizing a value everytime I use it

Hey! I’ve been making this template overhead script for a hour now, but I have a problem.

--// Services \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")

--// Variables \\--
local GUIPath = ReplicatedStorage.Storage.OverheadGUI
local NameTemplates = {
	["MTF"] = "[Operator-"..math.random(100, 200).."]",
	["ScD"] = "[Researcher-"..math.random(100, 200).."]",
	["Class-D"] = "[Prisoner-"..math.random(100, 200).."]"
}
local GroupTable = {
	['MTF'] = 0,
	['ScD'] = 0,
	['EC'] = 0,
	['E&T'] = 0,
	['Class-D'] = 0
}


--// Functions & Bindings \\--

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local OverheadGUI = GUIPath:Clone()
		OverheadGUI.Parent = Character.Head
		
		for key, value in pairs(NameTemplates) do
			if Player.Team.Name:match(key) then
				OverheadGUI.Frame.PlayerName.Text =  value
				OverheadGUI.Frame.PlayerName.TextColor3 = Player.TeamColor.Color
				OverheadGUI.Frame.Rank.Text = Player:GetRoleInGroup(GroupTable[key])
				OverheadGUI.Frame.Rank.TextColor3 = Player.TeamColor.Color
					
				if Player.Team.Name == "Class-D" then
					OverheadGUI.Frame.Rank.Visible = false
				else
					OverheadGUI.Frame.Rank.Text = Player:GetRoleInGroup(GroupTable[key])
				end
			end
		end
	end)
end)

This script actually gets the job done. But the problem is this part of the script:

local NameTemplates = {
	["MTF"] = "[Operator-"..math.random(100, 200).."]",
	["ScD"] = "[Researcher-"..math.random(100, 200).."]",
	["Class-D"] = "[Prisoner-"..math.random(100, 200).."]"
}

The randomized numbers are static. I want to make them randomize whenever I use them.

Any help is appreciated!

You could either define the table within the CharacterAdded or redefine it when the character is added.

Define when CharacterAdded

--// Services \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")

--// Variables \\--
local GUIPath = ReplicatedStorage.Storage.OverheadGUI

local GroupTable = {
	['MTF'] = 0,
	['ScD'] = 0,
	['EC'] = 0,
	['E&T'] = 0,
	['Class-D'] = 0
}


--// Functions & Bindings \\--

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
        local NameTemplates = {
            ["MTF"] = "[Operator-"..math.random(100, 200).."]",
            ["ScD"] = "[Researcher-"..math.random(100, 200).."]",
            ["Class-D"] = "[Prisoner-"..math.random(100, 200).."]"
        }
		local OverheadGUI = GUIPath:Clone()
		OverheadGUI.Parent = Character.Head
		
		for key, value in pairs(NameTemplates) do
			if Player.Team.Name:match(key) then
				OverheadGUI.Frame.PlayerName.Text =  value
				OverheadGUI.Frame.PlayerName.TextColor3 = Player.TeamColor.Color
				OverheadGUI.Frame.Rank.Text = Player:GetRoleInGroup(GroupTable[key])
				OverheadGUI.Frame.Rank.TextColor3 = Player.TeamColor.Color
					
				if Player.Team.Name == "Class-D" then
					OverheadGUI.Frame.Rank.Visible = false
				else
					OverheadGUI.Frame.Rank.Text = Player:GetRoleInGroup(GroupTable[key])
				end
			end
		end
	end)
end)

Re-define when character is added

--// Services \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")

--// Variables \\--
local GUIPath = ReplicatedStorage.Storage.OverheadGUI
local NameTemplates = {
	["MTF"] = "[Operator-"..math.random(100, 200).."]",
	["ScD"] = "[Researcher-"..math.random(100, 200).."]",
	["Class-D"] = "[Prisoner-"..math.random(100, 200).."]"
}
local GroupTable = {
	['MTF'] = 0,
	['ScD'] = 0,
	['EC'] = 0,
	['E&T'] = 0,
	['Class-D'] = 0
}


--// Functions & Bindings \\--

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
        NameTemplates = {
            ["MTF"] = "[Operator-"..math.random(100, 200).."]",
            ["ScD"] = "[Researcher-"..math.random(100, 200).."]",
            ["Class-D"] = "[Prisoner-"..math.random(100, 200).."]"
        }
		local OverheadGUI = GUIPath:Clone()
		OverheadGUI.Parent = Character.Head
		
		for key, value in pairs(NameTemplates) do
			if Player.Team.Name:match(key) then
				OverheadGUI.Frame.PlayerName.Text =  value
				OverheadGUI.Frame.PlayerName.TextColor3 = Player.TeamColor.Color
				OverheadGUI.Frame.Rank.Text = Player:GetRoleInGroup(GroupTable[key])
				OverheadGUI.Frame.Rank.TextColor3 = Player.TeamColor.Color
					
				if Player.Team.Name == "Class-D" then
					OverheadGUI.Frame.Rank.Visible = false
				else
					OverheadGUI.Frame.Rank.Text = Player:GetRoleInGroup(GroupTable[key])
				end
			end
		end
	end)
end)
1 Like

Defining when CharacterAdded is useful for me. Appreciate it!!!

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