Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

1 comment

0

Are you trying to search for a specific string then remove that element from the table?

1 answer

✓ Accepted answer

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

11 comments

0

The table got destroyed again @Azarth

0

It will only remove what it matches at that index '41, 42, 43'. Works fine for me. Does your table look like mine?

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

0

Can you re-edit your post and give an example as to how your table is structured?

0

line 40-51 in my new edit.

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.

0

xDDDDDD I know it is messy, datastores really arent my thing!

0

Wait, I may have found a way around this brb

0

YASSSS I DID IT, and you know what, I looped ItemPos xDDDDDD GG DUDE!

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.

0

Thanks dude! True true

This discussion is preserved as it was. New answers, comments, and votes are closed.