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

Is there a reason why my script wont change its number into text?

Asked by 9 years ago

If the value = 1 then I wont it to show Class names. The script is below.


P = game.Players.LocalPlayer while wait(1) do if P.bin2.ClassType.Value == 0 then P.bin2.ClassType.Value = math.random(1,20) if P.bin2.ClassType.Value == 1 then P.bin2.ClassType.Value = "Swordsman" elseif P.bin2.ClassType.Value == 2 then P.bin2.ClassType.Value = "Gunner" elseif P.bin2.ClassType.Value == 3 then P.bin2.ClassType.Value = "Mage" elseif P.bin2.ClassType.Value == 4 then P.bin2.ClassType.Value = "Berserker" elseif P.bin2.ClassType.Value == 5 then P.bin2.ClassType.Value = "Barbarian" elseif P.bin2.ClassType.Value == 6 then P.bin2.ClassType.Value = "Archer" elseif P.bin2.ClassType.Value == 7 then P.bin2.ClassType.Value = "Assassin" elseif P.bin2.ClassType.Value == 8 then P.bin2.ClassType.Value = "Conjurer" elseif P.bin2.ClassType.Value == 9 then P.bin2.ClassType.Value = "Enchanter" elseif P.bin2.ClassType.Value == 10 then P.bin2.ClassType.Value = "Elementalist" elseif P.bin2.ClassType.Value == 11 then P.bin2.ClassType.Value = "Gladiator" elseif P.bin2.ClassType.Value == 12 then P.bin2.ClassType.Value = "Ninja" elseif P.bin2.ClassType.Value == 13 then P.bin2.ClassType.Value = "Pirate" elseif P.bin2.ClassType.Value == 14 then P.bin2.ClassType.Value = "Priest" elseif P.bin2.ClassType.Value == 15 then P.bin2.ClassType.Value = "Spearman" elseif P.bin2.ClassType.Value == 16 then P.bin2.ClassType.Value = "Transmuter" elseif P.bin2.ClassType.Value == 17 then P.bin2.ClassType.Value = "Viking" elseif P.bin2.ClassType.Value == 18 then P.bin2.ClassType.Value = "Witch" elseif P.bin2.ClassType.Value == 19 then P.bin2.ClassType.Value = "Warrior" elseif P.bin2.ClassType.Value == 20 then P.bin2.ClassType.Value = "Wizard" end end end
0
What type of Value is it? DigitalVeer 1473 — 9y

1 answer

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

Your Problem

If you're using a value and it's holding a number then you're either using an IntValue or a NumberValue. These two objects cannot take in strings as their values! You need to have a separate StringValue to set the value to.


Efficiency Edits

You should make a dictionary for all the values. Have the keys be the classtype and have the values be the number. You can then iterate through the table, check if the number/intvalue is the value, and if so then set the StringValue to the key. It also looks much cleaner and professional.

You should also use a Changed event, rather than a while loop. A Changed event fires whenever said value changes, making it much more efficient.


Code

local P = game.Players.LocalPlayer
--Table for all classes. Key is the string, Value is the number.
local classes = {
    ['Swordsman'] = 1,
    ['Gunner'] = 2,
    ['Mage'] = 3,
    ['Berserker'] = 4,
    ['Barbarian'] = 5,
    ['Archer'] = 6,
    ['Assassin'] = 7,
    ['Conjurer'] = 8,
    ['Enchanter'] = 9,
    ['Elementalist'] = 10,
    ['Gladiator'] = 11,
    ['Ninja'] = 12,
    ['Pirate'] = 13,
    ['Priest'] = 14,
    ['Spearman'] = 15,
    ['Transmuter'] = 16,
    ['Viking'] = 17,
    ['Witch'] = 18,
    ['Warrior'] = 19,
    ['Wizard'] = 20
}

--Changed event on number value
P.bin2.ClassType.Changed:connect(function(change)
    local rand = math.random(1,#classes)
    local it = 0
    local key
    --iterate through table
    for i,v in next,classes do
        it  = it + 1
        if it == rand then key = i end
        --Check 
        if v == change then
            --Change StringValue
            P.bin2.StringValue.Value = i
        end
    end
    if change == 0 then
        P.bin2.StringValue.Value = key
    end
end
0
This script for some reason didn't work? attackonkyojin 135 — 9y
0
You need a separate StringValue object for the class .-. Goulstem 8144 — 9y
Ad

Answer this question