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

Scripting help with tables!?

Is there a way to simplify this script? I'm trying to make it so that if the player's stat is # then it would change text to "Class : (Number)"


while true do
	wait()
 if game.Players.LocalPlayer.bin.Class.Value == 1 then

script.Parent.Text = "Class : Swordsman"

elseif
	
	game.Players.LocalPlayer.bin.Class.Value == 2 then
	
	script.Parent.Text = "Class : Gunner"
	
elseif
	
	game.Players.LocalPlayer.bin.Class.Value == 3 then
	
	script.Parent.Text = "Class : Mage"
elseif
	
	game.Players.LocalPlayer.bin.Class.Value == 4 then
	
	script.Parent.Text = "Class : Berserker"
elseif
	
	game.Players.LocalPlayer.bin.Class.Value == 5 then
	
	script.Parent.Text = "Class : Berserker"
elseif
	
	game.Players.LocalPlayer.bin.Class.Value == 6 then
	
	script.Parent.Text = "Class : Berserker"
end
end

1 answer

✓ Accepted answer
--variables
local player = game:GetService('Players').LocalPlayer
local replicated = game:GetService('ReplicatedStorage')
local class = player:WaitForChild('bin').Class

--table
local classes ={
	[1]="Swordsman";
	[2]="Gunner";
	[3]="Mage";
	[4]="Berserker";
	[5]="Barbarian";
	[6]="Archer";
	[7]="Assassin";
	[8]="Conjurer";
	[9]="Enchanter";
	[10]="Elementalist";
	[11]="Gladiator";
	[12]="Ninja";
	[13]="Pirate";
	[14]="Paladin";
	[15]="Priest";
};

fetch = function (parent, query)
	for i, child in ipairs(parent:GetChildren()) do
		--loop through the parent "argument"
		if child:IsA('Model') or child:IsA('Folder') then
			-- if child is a "Model" or "Folder" 
			if child.Name:upper() == query:upper() then
				-- if "Name" property matches "query" argument
				return child
			end
		end
	end
end

class.Changed:connect(function (property)
	if property == 'Value' then
		-- if the changed property is the class' `Value` property
		for index, value in ipairs(classes) do
			-- loop through table `classes`
			if class[property] == index then
				-- if value of `class` equals `index`

				print(index, value)
				script.Parent.Text = value
				-- set text to `value`

				folder = fetch(replicated, tostring(index))
				print(folder:GetFullName())
			end
		end
	end
end

I used the Changed event instead of your while loop because it would be much more efficient considering that it only fires when the object/instance is changed instead of continually.

2 comments

0

Thx and would "{[1]="Swordsman",[2]="Gunner",[3]="Mage",[4]="Berserker",[5]="Barbarian",[6]="Archer", [7]="Assassin",[8]="Conjurer",[9]="Enchanter",[10]="Elementalist",[11]="Gladiator",[12]="Ninja", [13]="Pirate",[14]="Paladin",[15]="Priest"}" work over in the class table?

0

yes

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