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

This is really confusing to me How do I make a i,v in pairs loop ,loop a string value using v?

Asked by 5 years ago
Edited 5 years ago

So this script is a data saving backpack script Inventory is a Folder that holds all the items this folder is in the serverstorage Classification is a StringValue to Classify the item name I added it because I tend to change the name of my tool sometimes by mistake and as you know this script uses SetAsync() which means once I join a server with changed name it will be gone forever

as you can see im looping using v but on line 6 it saids and im trying to figure out how I can get this i,v in pairs to loop and get to Classification

local weopon = game.ServerStorage.Inventory:FindFirstChild(v):Clone()

so that it syncs with

    table.insert(Table,v:WaitForChild('Classification').Value)--insert whatever is found  in the table

local DataStore = game:GetService("DataStoreService"):GetDataStore('ToolSave')--
game.Players.PlayerAdded:Connect(function(plr) --when the player is added to the game in the player properties
    local savedstuff = DataStore:GetAsync(plr.userId)-- saved stuff variable means The Data of the player using userID
    if savedstuff ~= nil then --if the data is not nil continue this script
        for i,v in pairs(savedstuff) do --for i,v in pairs the Data Inventory 
            if game.ServerStorage.Inventory:FindFirstChild(v) ~= nil then --Try looop and try find the inventory and if its not nil then continue
                local weopon = game.ServerStorage.Inventory:FindFirstChild(v):Clone() --if it isnt nil the clone it
                weopon.Parent = plr:WaitForChild('Backpack')-- Puts this in the backpack 

            end
        end
    end
plr.CharacterRemoving:Connect(function()-- When character is being removed from the game
    plr.Character.Humanoid:UnequipTools()-- Un equip the weopons 

end)
end)

game.Players.PlayerRemoving:Connect(function(plr)-- when player is being removed
    local Table = {}-- Creates a table
for i,v in pairs(plr.Backpack:GetChildren()) do-- for i,v in pairs loop around the backpacks childrens
    table.insert(Table,v:WaitForChild('Classification').Value)--insert whatever is found  in the table

end
if Table ~= nil then-- if table is not nil when the player leaves the game save the game
    DataStore:SetAsync(plr.userId,Table)--  if it is not equal to nil then Set the Data to that using user iD and putting table into it 
end
end) 
0
the first argument of FindFirstChild should be a string, not an object (line 7) theking48989987 2147 — 5y
0
if v is a stringvalue just do v.Value User#22604 1 — 5y

1 answer

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

I think you've mistaken v for the table it gets when you do the for i,v in pairs. For i,v in pairs just makes so you can say like v.Value = v.Value + 10 and it will do that for every child in the table. So do something more like . . .

for i,v in pairs(plr.Backpack:GetChildren()) do -- Gathers the children of player.Backpack.
    if v.Name == "Classification" then -- Checks every child in the backpack to see if its name is "Classification".
        local classification = v.Value -- Sets a variable to be the value of the string.
    end
end

It just repeats the command with v being the child it's on. So it would check all of the children and for all of the children that have the name of "Classification", it would do whatever you need it to. Hope this helped!

0
Sorry if I didn't explain it very well, it's kind of confusing at first, and weird to explain. Knineteen19 307 — 5y
Ad

Answer this question