Use a string as a reference to find a variable

I was wondering if there is any way I can get a variable or a table and automatically use it by name , for example if a person is on the Blue team , when I use the code I use the person 's team name or color as a reference for find the variable entered above. Here is an example of what i was trying to do

local Dio = {
	Taunt1 = {Text = "Too slow, too slow!", SoundID = "rbxassetid://4580051622", Volume = .5},
	Taunt2 = {Text = "Weak! Weak!", SoundID = "rbxassetid://4580050174", Volume = .5}
}

local function RandomVoiceFind (Team,RandomChoice,Sound,Texto)
	
	local Taunt = "Taunt"..RandomChoice    --RandomChoice is a system for randomizing speech (math.random)

	Sound.SoundId = Dio[Taunt].SoundID  -- the problem is here, I wanted to remove this "Dio" to leave it as the name of the team to make a more practical system

	Sound.Volume = Dio[Taunt].Volume
	Sound:Play()
end

what can I do to get the person’s team name and look for a table with the same name using it as a reference in the script? I’m sorry if it’s hard to understand it’s my first time posting here and I’m also a beginning programmer.

1 Like

that is the only way to get a value using a value (string in this case)
table[value]

unless ofc if there is a function that does that

1 Like

so I should do a function like this to find the table?

local function GetTeamName(TeamName)
	local Team = {}
	if TeamName.Name == "Dio" then	
		Team = Dio
	elseif TeamName.Name == "Lielmaster" then
		Team = Lielmaster
	end
	return Team
end 

probably yes.

But if the indexes of your table is always the team name then you could just directly index it using that

--Teams  Red, Blue Green

value = Table[TeamName.name][index]
-- instead of
if TeamName.Name == "Dio" then
    ...
end

if not then you could just directly return the value instead of assigning a variable to it

local function GetTeamName(TeamName)
	if TeamName.Name == "Name" then
		return "Name"
	end
end
1 Like

I think i know what you are trying to do, let’s see if this is correct:

You are trying to get the Player’s Team, and [Based on the Specific Team] it will do some Specific action:

I’ll create an example here

First, create a table with teams:


local Teams = {
	
	["Dio Team"] = {
		
		Taunt1 = {Text = "Too slow, too slow!", SoundID = "rbxassetid://4580051622", Volume = .5},
		Taunt2 = {Text = "Weak! Weak!", SoundID = "rbxassetid://4580050174", Volume = .5}	
	},
	
	["Jojo Team"] = {
		Taunt1 = {Text = "Too slow, too slow!", SoundID = "rbxassetid://4580051622", Volume = .5},
		Taunt2 = {Text = "Weak! Weak!", SoundID = "rbxassetid://4580050174", Volume = .5}	
	},
	
	
	["Johnny Team"] = {
		Taunt1 = {Text = "Too slow, too slow!", SoundID = "rbxassetid://4580051622", Volume = .5},
		Taunt2 = {Text = "Weak! Weak!", SoundID = "rbxassetid://4580050174", Volume = .5}
	}
	
}

--Teams = Table

--Inside Teams we have a list of "Teams that we created"

--Inside each Specific Team, we have some Values stored (can be anything)




-- Since i don't know from where those arguments are coming: "Team" [[ then i will consider them as properly functional ]]

local function RandomVoiceFind(Team,RandomChoice,Sound,Texto)

	local Taunt = "Taunt"..RandomChoice   

	Sound.SoundId = Teams[Team][Taunt].SoundID 
	
	--Teams is the Table
	--Team is The [Player Team]
	--Taunt is inside the Player Specific Team, so it will grab the Specific Taunt designed
	--SoundId will happen the same thing
	
	Sound.Volume = Teams[Team][Taunt].Volume
	Sound:Play()
end

3 Likes

I learned how it works, thanks to both of you, you guys helped me a lot!