Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Is there an easy way to do this?

Asked by 9 years ago

Lets say I want a model to be called "Car One" based on a value (1) so if the value is 4, the model will be called "Car Four". I'm wondering if there's a shortcut to turn number values to words.

1 answer

Log in to vote
3
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

You can easily do this by concatenation. But first, you need to make a Dictionary. Dictionaries are essentially tables with keys and values.

So, lets make a dictionary with keys of the full word, and values of the number. Then according to the number value, iterate through the table. If there's a match then concatenate the key with the model name.

local nums = {
    ['One'] = 1,
    ['Two'] = 2,
    ['Three'] = 3,
    ['Four'] = 4
}

local numVal = workspace.NumberValue --Just an example
local model = workspace.Car

for i,v in pairs(nums) do
    if v == numVal.Value then
        model.Name = model.Name..i
    end
end
0
thanks BSIncorporated 640 — 9y
1
Doesn't it make more sense to go the other way? words = { "One", "Two", "Three", "Four" } ?? BlueTaslem 18071 — 9y
0
Yeah, you can do that then just index it like nums[i]. But I thought this might be more understandable, as well as it teaches them about Dictionaries(: Goulstem 8144 — 9y
Ad

Answer this question