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

remove string from table, deleting the entire table, help please ;( ??

Asked by 5 years ago
Edited 5 years ago

Suppose I have a table

{"31,32,33","41,42,43","20,19,18"}

And I want to get rid of one of those strings, lets say "41,42,43".... I tried doing this:

table.remove(ThatTable, Vector3.new(41,42,43))
table.remove(ThatTable, tonumber(41,42,43))
table.remove(ThatTable, tostring(41,42,43))
table.remove(ThatTable, "41,42,43")

It just won't work, could I have some help please?

I have a datastore script that creates a table for me and I am trying to remove the string

For Azarth:

--Datastore:SetAsync(key,{pos,name,otherdata,moredata})
repeat wait() print("Player not yet loaded...") until script.Parent.GetPlayer.Value == true print("Player Loaded")
wait(1)
local tbl = {} or nil
wait(.5)
local DataStore = game:GetService("DataStoreService"):GetDataStore("Vector_Saver")
local lastPos = Vector3.new(0,0,0)
-- getting player
for _, v in pairs(workspace:GetChildren()) do
    if v:FindFirstChild("Player") then
        local name = v.Player.Value
        print(name.." has joined the game :D")
        Plr = game.Players:FindFirstChild(name)
    end
end
-- If the datastore has nothing in it, it will error so I am putting 0,0,0 as the starting coordinates
repeat wait() until Plr
--DataStore:SetAsync(Plr.UserId,tbl)
if DataStore:GetAsync(Plr.UserId) == nil then
    table.insert(tbl,"000")
    DataStore:SetAsync(Plr.UserId,tbl)
else
end
DataStore:GetAsync(Plr.UserId)
-- setting data / saving it
for _, v in pairs(DataStore:GetAsync(Plr.UserId)) do
    print(v)
end
game.ReplicatedStorage.Events.SavePos.OnServerEvent:Connect(function()
    if Plr then
        for _, v in pairs(Plr.HiddenStats.ItemPos:GetChildren()) do
            lastPos = v.Value
            table.insert(tbl, tostring(v.Value))
        end
        DataStore:SetAsync(Plr.userId, tbl)     
    else
        wait()
    end
end)
script.Parent.SellEvent.Changed:Connect(function()
    if Plr then
        for _, v in pairs(Plr.HiddenStats.ItemPos:GetChildren()) do
            table.remove(DataStore:GetAsync(Plr.UserId), script.Parent.SellEvent.Pos.Value.X)
            table.remove(DataStore:GetAsync(Plr.UserId), script.Parent.SellEvent.Pos.Value.Y)
            table.remove(DataStore:GetAsync(Plr.UserId), script.Parent.SellEvent.Pos.Value.Z)
        end
        DataStore:SetAsync(Plr.userId, tbl)     
    else
        wait()
    end
end)
-- end of script :)
-- finally got it to work after 4 years :D
-- in real game, there is problem with Saving.... DUE TO EveNTS NOT CHANGING IN REAL GAME MODE!
0
Are you trying to search for a specific string then remove that element from the table? Impacthills 223 — 5y

1 answer

Log in to vote
1
Answered by
Azarth 3141 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You can only directly 'lookup' a string like that in a dictionary. You'd have to loop through the table otherwise. Here are some options.

local thatTable = {"31,32,33", "41,42,43","20,19,18"}

table.remove(thatTable, 2 )
print( unpack(thatTable) )


-- or 

for i,v in pairs(thatTable) do
    if v == "41,42,43" then 
        table.remove(thatTable, i)
    end
end

-- or 

thatTable = { ['31,32,33'] = true, ['41,42,43'] = true, ['20,19,18'] = true}

thatTable[ '41,42,43' ] = nil

for i,v in pairs(thatTable) do
    print(i)
end

0
The table got destroyed again @Azarth greatneil80 2647 — 5y
0
It will only remove what it matches at that index '41, 42, 43'. Works fine for me. Does your table look like mine? Azarth 3141 — 5y
0
I am using a datastore table, and it is a placement system datastore table, I save the vectors as strings then tried what you did greatneil80 2647 — 5y
0
Can you re-edit your post and give an example as to how your table is structured? Azarth 3141 — 5y
View all comments (7 more)
0
line 40-51 in my new edit. greatneil80 2647 — 5y
0
Oh man, I can't say for sure. Looks like you need to be looping thrugh your table though, not ItemPos as you're not even using the v variable in your loop. Azarth 3141 — 5y
0
xDDDDDD I know it is messy, datastores really arent my thing! greatneil80 2647 — 5y
0
Wait, I may have found a way around this brb greatneil80 2647 — 5y
0
YASSSS I DID IT, and you know what, I looped ItemPos xDDDDDD GG DUDE! greatneil80 2647 — 5y
0
Lol good job. You should try to read up on keys and values and the table api on the lua documentation page. Could of been simpler kinda looks like you're overthinking here. Impacthills 223 — 5y
0
Thanks dude! True true greatneil80 2647 — 5y
Ad

Answer this question