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

-Variable- is not a valid member of Model?

I've been getting back in to Roblox development and I've been doing super good at remembering the thing I've learned (7+ years ago ha-ha). I've run into a snag that I'm super confused by. I'll break it down within my code:

Animals = {"Pig", "Cow", "Chicken", "Sheep"}
ChosenAnimal = Animals[math.random(1,#Animals)]
			
Animal = script.Parent.Parent.Parent.ChosenAnimal
AnimalPrice = Animal.Price
Cash = script.Parent.Parent.Parent.Money

So basically, this is waiting for a Humanoid model to hit the block, which works beautifully. Then, I'm wanting to get the script to pick from a list of four animals and use that value in the "Animal" variable as you can see above.

I'm getting really frustrated, because if I use script.Parent.Parent.Parent.Pig, it operates as desired. It's only when I add the variable 'ChosenAnimal' into the end that I get the error.

"12:07:18.024 - ChosenAnimal is not a valid member of Model"

I assume that the 'Animal' line is having trouble pulling in the 'ChosenAnimal' variable, but I have no idea how to fix it and haven't found anything elsewhere to help me out. Please help?? :)

1 answer

Syntax sugar
Bittersweet

Using the syntax t.k is short for t["k"] - The solution in this case is simple:

Animal = script.Parent.Parent.Parent[ChosenAnimal]

Or even

Animal = script.Parent.Parent.Parent:FindFirstChild(ChosenAnimal)

3 comments

0

I could kiss you! Thank you! :)

0

Random sub question... Does printing to console contribute at all to any game lag? I would leave the prints I have in this script running but there are at least 6/minute generated and I don't want to cause unnecessary lag if that's what it will do.

0

Shouldn't introduce any lag, and prints are only visible to people who enjoy looking at the console.

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