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!
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!
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!