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

CV,NA is not creating my BoolValues or NumberValues, Why?

Asked by
Kitorari 266 Moderation Voter
6 years ago
local D = game:GetService("DataStoreService"):GetDataStore("AI")
local HS = game:GetService("HttpService")

game.Players.PlayerAdded:connect(function(P)
    wait(1)
    local F = Instance.new("Folder")
    F.Parent = P
    F.Name = "Values"

    local CV,NA = {
        Money = "BoolValue","Money";
        Axe = "BoolValue","Axe";
        AppleCount = "NumberValue","AppleCount";
        Bread = "NumberValue","Bread";
        Bucket = "BoolValue","Bucket";
    }

    for i = 1,5 do
        if CV == "BoolValue" then
        local B = Instance.new("BoolValue").Parent == P.Values
        B.Name = NA
        elseif CV == "NumberValue" then
            local N = Instance.new("NumberValue").Parent == P.Values
            N.Name = NA
        else print("Error")

    end

    local K = P.UserId
    D:GetAsync("AI", K)
    end


end)

After solving all the errors, for some reason "I = 1,5 do" Isn't actually creating the values. Is there something I did wrong around there?

What I'm basically trying to do is when the player joins the game, For each of CV,NA's values, they create a NumberValue or a BoolValue, depending on CV. then it's named according to NA.

The output when I run this code, just prints "Error".

But I've checked my "if" statements, and I don't understand why it's not confirming/Creating the instances according to CV....

Any help would be appreciated.

0
youre looping through it wrong abnotaddable 920 — 6y
0
You should indent your code correctly. http://wiki.roblox.com/index.php?title=Lua_Style_Guide#Whitespace_Rules BlueTaslem 18071 — 6y
0
How am I looping through it wrong? and correctly? I thought putting .parent at the end of a instance would make the code shorter? Kitorari 266 — 6y
0
Try making variables more meaningful, instead of just like P for player. Also, where did you get CV from? hiimgoodpack 2009 — 6y
View all comments (2 more)
1
P is the acronym for player, though. TheDeadlyPanther 2460 — 6y
0
Normally I shorten Variable names to make the code look less... Messy, and shorten the lines of code so I can operate in a two-window mode. CV stands for Character Variables. Kitorari 266 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

You are trying to loop through a table wrong.

for Index = 1,5 do
    print(Index)
end

This would print 1 through 5. A loop like this does nothing else, the only thing that changes is Index (AKA i).

What you have to do is actually loop through the table itself:

local Table = {
    Money = {"BoolValue","Money"};
    -->> You can't assign a value to multiple values, put these multiple values into a table instead
}
-->> One value = one table, you can't split a table up into multiple values
-->> In your code, doing so would make NA nil

for _,Data in pairs (Table) do -->> Loops through table with Data being the entry in the table
    local CV,NA = Data[1],Data[2] -->> Get the first and second values in the table
    local Value = Instance.new(CV) -->> No need for if statements, just use the CV
    Value.Name= NA -->> Same thing here
    Value.Parent = F
    -->> This is how you set a parent, don't use ==
    -->> Also use F since it is better than using P.Values again
end

Tips

Formatting

Make sure to format properly - it is super hard to debug/read your code when it's not formatted properly.

GetAsync

Your GetAsync is useless, and does not work.

This is what you do to make it work:

D:GetAsync(K) -->> GetAsync only has 1 value you can give it, and it is the key

But, it also is useless because you are getting the saved data, but not saving it anywhere.

local SavedData = D:GetAsync(K) -->> Set it to a variable

Hope I helped!

~TDP

0
Thank you! Kitorari 266 — 6y
Ad

Answer this question