How do I change the bool value on an unknown NPC

Hello everyone,

Thank you in advance for taking the time to read this.

I am newish to Roblox and I am trying to make a game that displays a random math problem on the screen. Numbers are running around the map and you have to hunt down the answer to the problem and shoot it to get cash. I have almost everything working great, but I have been stuck on this core mechanic for about a week now. I don’t know how to assign the correct answer tag to the correct NPCs so that they award the prize.

I have the script setup to give you $50 for any number you shoot (so I know that is working), but I want it to give you $500 if you shoot the number that is the correct answer.

First I tried to set a bool value to all NPCs and have it changed to true when it was the correct answer while the bots were also named the correct answer, but I don’t know how to recognize the bot to flag it and all it’s respawns.

Then, I came up with an idea to make the key equal to the answer, but that seriously limited my ability to add hundreds of problems and each answer could only have 1 problem, but I couldn’t get even that to work.

Then I tried to setup a matrix by having a table made up of all the answers that were tables to the matching problems, but I got so confused I deleted it all.

Then I tried the approach of using a dictionary for the first time, but I didn’t know how to pull random problems from it and still couldn’t get the bool value to switch. This code has been changed about 30 times so, honestly so I can’t tell you what it does now or if I’m even in the right direction. Please help.

local grade1 = {"0 + 1 = ? ", "1 + 1 = ? ", "0 + 3 = ? ", "2 + 2 = ? ", "2 + 3 = ? "}
--pull a random problem from the table, iterate the loop 5 times
	for i = math.random(1,#grade1), 5, -1 do
		local v = grade1[i]  -- set v to be the math problem and display it to the GUI
		status.Value = v
		game.Workspace.BaseMap.Bots.i.answer.Value = true  -- key is equal to the answer, change bool value to true to grant bonus
		wait(20)												-- time to solve
		game.Workspace.BaseMap.Bots.i.answer.Value = false  -- change bool back to false to remove bonus
	end

Thank you again for reading and any suggestions in advance.
-Danny

*edit I forgot to include that I have a remote function that triggers the number assigned to a variable and I set up an if statement that checks the bool value of answer to determine which value to assign to amount, but I don’t think there is anything wrong with this code.
*edit I forgot to put the errors. It says in the output that i is not a valid member. I also tried “i”.
*edit here is a link to the game if you would like a visual demonstration. Most things are placeholders or still need detail and the math problems are not active currently to test other game mechanics.

You can’t use a variable in a path like you have done. It’s looking for a bot named i. The way you can access a child by a variable is by putting it’s name in brackets by the parent.

game.Workspace.BaseMap.Bots[tostring(i)].answer.Value = true
wait(20)
game.Workspace.BaseMap.Bots[tostring(i)].answer.Value = false

It’s worth noting that your for loop will either run one time or try to run forever. It currently gets a random number as the initial value of i, then attempts to count down until it reaches 5. Once it gets to 0 your code will throw an error.

My recommended code:

local grade1

grade1.questions = {"0 + 1 = ? ", "1 + 1 = ? ", "0 + 3 = ? ", "2 + 2 = ? ", "2 + 3 = ? ", "1 + 3 = ? "}
grade1.answers = {1, 2, 3, 4, 5, 4}

--pull a random problem from the table, iterate the loop 5 times
for i=1, 5 do
	local randomQuestionIndex = math.random(1,#grade1.questions)
	status.Value = grade1.questions[randomQuestionIndex]
	
	game.Workspace.BaseMap.Bots[tostring(grade1.answers[randomQuestionIndex])].answer.Value = true  -- key is equal to the answer, change bool value to true to grant bonus
	wait(20)												-- time to solve
	game.Workspace.BaseMap.Bots[tostring(grade1.answers[randomQuestionIndex])].answer.Value = false  -- change bool back to false to remove bonus
end

If you have multiple bots with the same number though you would need to iterate through them all to set the values, wait, then iterate back to disable them.

1 Like

That is deep, but I get it now.
Instead of putting the tables inside one another, they can be linked sort of parallel.

I see what you mean on the for loop now, too.

Thank you a bunch!

I just have to figure out how to make the rounds cycle through the grade levels and all of my scripting for core mechanics are done!

-Danny

Hey real quick,

I got this error message:
[ServerScriptService.Script:68: attempt to index local ‘grade1’ (a nil value)]
so I changed the first line to grade1 = {} and that seems to work, but on the iteration of 2 bots with the same name I tried:

local grade1 = {}

grade1.questions = {"0 + 1 = ? ", "1 + 1 = ? ", "0 + 3 = ? ", "2 + 2 = ? ", "2 + 3 = ? ", "1 + 3 = ? "}
grade1.answers = {1, 2, 3, 4, 5, 4}

--pull a random problem from the table, iterate the loop 5 times
for i=1, 5 do
	local randomQuestionIndex = math.random(1,#grade1.questions)
	status.Value = grade1.questions[randomQuestionIndex]
	for i = 1, 2 do
	game.Workspace.BaseMap.Bots[tostring(grade1.answers[randomQuestionIndex])].answer.Value = true  -- key is equal to the answer, change bool value to true to grant bonus
	wait()
	end
	wait(20)												-- time to solve
	for i = 1, 2 do
	game.Workspace.BaseMap.Bots[tostring(grade1.answers[randomQuestionIndex])].answer.Value = false  -- change bool back to false to remove bonus
	wait()
	end
end

However it is still only changing one of the bots to true. Is there a work around or am I messing up the for loop again?

Thanks!

On 2nd thought, our original design was 1 bot everyone had to fight over, but the bots were too easy so we doubled them and increase the spawn timer.

Now it is almost too hard for a novice player and less fun. I will revert back to a single bot per small round map and avoid the mix up altogether.

If we had a server full of 10 people plus 10 bots, that is a lot of lead flying around. I could also scale the maps up and go up to number 20 if I must add additional bots, I shouldn’t be lazy about it.

Thanks!
-Danny

game.Workspace.BaseMap.Bots[tostring(grade1.answers[randomQuestionIndex])].answer.Value = true

This will actually only affect the first bot no matter how many times it’s ran. You should loop through all of the bots setting each of their values. If done this way you can use a single loop to change it.

local grade1 = {}

grade1.questions = {"0 + 1 = ? ", "1 + 1 = ? ", "0 + 3 = ? ", "2 + 2 = ? ", "2 + 3 = ? ", "1 + 3 = ? "}
grade1.answers = {1, 2, 3, 4, 5, 4}

--pull a random problem from the table, iterate the loop 5 times
for i=1, 5 do
	local randomQuestionIndex = math.random(1,#grade1.questions)
	status.Value = grade1.questions[randomQuestionIndex]
	
	for _, bot in pairs(game.Workspace.BaseMap.Bots:GetChildren()) do --Loops through all children of Bots.  Each is accessed by the variable bot
		bot.answer.Value = (bot.Name == tostring(grade1.asnwers[randomQuestionIndex])) --True if bots name is equal to answer, false if not
	end 
	wait(20)												-- time to solve
end
1 Like

Okay, I see how that works.
That will still be helpful if I expand bigger maps to include double bots. Right now I think 1 is sufficient if I just slightly turn up the difficulty.

	for _, bot in pairs(game.Workspace.BaseMap.Bots:GetChildren()) do --Loops through all children of Bots.  Each is accessed by the variable bot
		bot.answer.Value = (bot.Name == tostring(grade1.asnwers[randomQuestionIndex])) --True if bots name is equal to answer, false if not
	end 

That also helps me figure out how to get multiple correct answers for factor or multiples of a # questions.

Can I give you a double solve? IDK
I don’t want to take up your whole night and I have TONs of stuff to do now.
I was really getting frustrated and burnt out at this.
I’ll have to credit you in the game if you don’t mind.