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

Having An Issue With A Table In Storing A String And A Number?

Asked by
fff054 51
5 years ago

I'm Trying To Make An Automatic System Which Gives You The Correspondent LightSaber. I Made A Table To Store The Name Of The LightSaber And The Reputation You Need To Get It. Here's The Script:

local SabersMainModule = {}

local Sabers = game.ReplicatedStorage.LightSabers:GetChildren()
for i, v in pairs(Sabers) do
    if v then
        table.insert(SabersMainModule, v)
    end
end

function SabersMainModule.GiveSaber(plr)
    repeat wait() until plr.Character
    local Kills = plr.leaderstats:WaitForChild("Reputation").Value
    local SabersKills = {
        {'Cross-GuardedSaber', -120};
        {'RedSaber',-75},
        {'WhiteSaber',-20},
        {'BlueSaber',0},
        {'GreenSaber',20},
        {'YellowSaber',75},
        {'RainbowSaber',120},
        {'GoldenSaber',160},
    }

    local CurrentSaber 
        for i, v in pairs(SabersKills) do
            if v then
                if Kills >= v then
                if SabersKills[i+1] > Kills then
                CurrentSaber = v
                print("Checking")
                return CurrentSaber
                else
                print("Not This Value")
                end
            end
        end
    end
end

return SabersMainModule

The Problem Is That When I Test It, An Error Appears Saying That I Can't Compare Strings With Numbers. That Means That The Loop Is Ignoring The Numbers In The Table Next To The LightSaber. If Anyone Knows How To Make That The Loop Gets The Numbers Not The Strings, Please Tell Me. Thanks!

0
maybe you can comment in scripting helpers, they respond with information that is above average Agus550 0 — 5y

2 answers

Log in to vote
0
Answered by
vexound 170
5 years ago
Edited 5 years ago

What you may want to do here is use what is known as a dictionary. A dictionary is general-purpose data structure for storing groups of objects with keys and values associating with these keys. I'd recommend using one for keeping track of the SaberKills for each saber. So replace you're current table with this:

local SabersKills = {
    ["Cross-GuardedSaber"] = -120;
    ["RedSaber"] = -75;
    ["WhiteSaber"] = -20;
    ["BlueSaber"] = 0;
    ["GreenSaber"] = 20;
    ["YellowSaber"] = 75;
    ["RainbowSaber"] = 120;
    ["GoldenSaber"] = 160;
}

The characters to the left of the equal sign represent the key while the characters to the right of it represent the value associated with the key. In order to reference a value we must first reference the dictionary then the key. So lets say we wanted to get the value for RedSaber, then we'd write out SaberKills["RedSaber"] and it'd return the corresponding value which is -75.

Next we need to fix your generic for loop. In this case the i (iteration) would be the key in the dictionary while v would be the corresponding value. So to make this work properly we'd write it like this

        for i, v in pairs(SabersKills) do
                 if v then
                        if Kills >= v then
                            if v > Kills then
                                CurrentSaber = i
                                print("Checking")
                                return CurrentSaber
                            else
                                print("Not This Value")
                            end
                        end
                end
        end

Hope this helped and if it did be sure to mark the answer as answered. Have a nice day!

0
Thanks For Your Answer But That Wasn't What I Was Looking For. When I First Tried To Do This Script, I First Tried With A Dictionary But After Searching On Google, I Discovered That You Can't Loop Through A Dictionary, So What I Want To Do It's Not Possible With A Dictionary. Anyways, Thank You Again For Your Answer. fff054 51 — 5y
0
You can loop through a dictionary. It's just that when looping through dictionaries there is no order when iterating through. What you want is definitely possible with dictionaries, just try to re-read what i posted and if it still isn't helping then refer to this https://developer.roblox.com/en-us/articles/Table vexound 170 — 5y
0
Thank You! I Knew That You Could Loop Through A Dictionary, But The Values Just Aren't In Order. I'll Try To Fix The Script By My Own, But Thank You Too Much For Clarifying Some Concepts. I Wish You The Best. fff054 51 — 5y
0
I wish you the best too vexound 170 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Ok, lets try it!

First Step : First of all you need to know the differenc between a normal table and a Dictionarie. You're trying to do a Dictionarie so lets try to fix your code!

Second Step : We need to know how to use the Dictionarie table. So lets use a expample. And see what happens!

Third Step : Enjoy coding! You always have to enjoy what are you doing! Play some music while you script or even play a game before so you will be relaxed.

Lets get into the code!

local tab = {
    ["Red Saber"] = 1, --> Use a comma to go to the next line of code
    ["Blue Saber"] = 2,
    ["Yellow Saber"] = 3,
    ["Green Saber"] = 4
}

print(tostring(tab["Red Saber"]))

--> Output : 1

So know we know how to use the Dictionarie tabs, now we can apply it to your code! The only thing you have to do is change the lines 14-21 how I already teach you. And then you will be fine!

I hop that my answer already solve your problem. If its like that, please accept my answer. That will help a lot!

Keep scripting!

0
Thanks For Answering My Question, But As I Told The Guy Below Your Answer, That's Not What I Was Looking For. You Can't Loop Through A Dictionary Because It Dosen't Have An Order. So You Can't Know What Value Goes Next, And If I Can't Know That, Then I Can't Know Which LightSaber Corresponds To A Player. Again, Thank You! fff054 51 — 5y

Answer this question