Invalid argument #2 to 'remove' (number expected, got string)

Yes, I am making my script, but it can’t run.

Script:

local Words = {
			"yes",
			"roblox",
			"good",
			"nice",
			"hate",
			"like",
			"sleep",
			"yo",
			"chat",
			"say"
		}
	
for i,v in pairs(game.Players:GetPlayers()) do
	local Word = Words[math.random(1, #Words)]

	if #Words == 0 then break end
		
	if v.Character then
		v.Character.Head.Text.Text.Text = Word
		game.ReplicatedStorage.Events.RemoteEvent:FireClient(v, "46T4GkhxgvMFHH9JL8KB", v.Character)
	end
	table.remove(Words, Word) -- invalid argument #2 to 'remove' (number expected, got string)
end

You need to give table.remove the index to remove, not the value. Since you want a random word you should keep track of what number you generated so you can use it to remove index.

local WordPos = math.random(#Words)
local Word = Words[WordPos]

-- ...

table.remove(Word, WordPos)
2 Likes

Hmm, the script still cant run but thank you for help
I am reading the API of roblox

Try this: table.remove(Words, table.find(Words, Word)) , table.find returns the position of an item in a table

1 Like

I know what you mean, but it still not work.

Does it error though? I tried it out for myself and it works.

May I ask why are you looping through the entire table just to check if one item equal to a value when table.find already exists?


Explain how it doesn’t work.

2 Likes

The script not work and does not have any error

I’ll repeat myself. Explain and be specific how to script doesn’t work rather than be vague and answer that it just doesn’t work.

I make a test at the bottom.

local Words = {
		"yes",
		"roblox",
		"good",
		"nice",
		"hate",
		"like",
		"sleep",
		"yo",
		"chat",
		"say"
	}
		
for i,v in pairs(Words) do
	local Word = math.random(#Words)
			
	if #Words == 0 then break end
			
	if v.Character then
		v.Character.Head.Text.Text.Text = Word
		game.ReplicatedStorage.Events.RemoteEvent:FireClient(v, "46T4GkhxgvMFHH9JL8KB", v.Character)
	end
	table.remove(Words, table.find(Words, Word))
end
print("Loop finished")

It did not print

Sorry, I did not send the full script.

Replace
local Word = Words[math.random(1, #Words)]
With
local Word = math.random(1, #Words)
Also,
Replace
v.Character.Head.Text.Text.Text = Word
With
v.Character.Head.Text.Text.Text = Words[Word]

2 Likes
local Words = {
		"yes",
		"roblox",
		"good",
		"nice",
		"hate",
		"like",
		"sleep",
		"yo",
		"chat",
		"say"
	}
		
for i,v in pairs(Words) do
	local Word = math.random(#Words)
			
	if #Words == 0 then break end
			
	if v.Character then
		v.Character.Head.Text.Text.Text = Word
		game.ReplicatedStorage.Events.RemoteEvent:FireClient(v, "46T4GkhxgvMFHH9JL8KB", v.Character)
	end
	table.remove(Words, 1)
end
print("Loop finished")

I think that should work.

1 Like

That is the problem
The Words won’t 0, so what can I do?

local Remote = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("RemoteEvent")
local Words = {
	"yes",
	"roblox",
	"good",
	"nice",
	"hate",
	"like",
	"sleep",
	"yo",
	"chat",
	"say"
}


for i, Player in pairs(game.Players:GetPlayers()) do
	if #Words == 0 then break end
	local WordIndex = math.random(1, #Words)
	local Word = Words[WordIndex]
	local Character = Player.Character
	if Character then
		local Head = Character:WaitForChild("Head")
		if Head then
			local Text1 = Head:WaitForChild("Text")
			if Text1 then
				local Text2 = Text1:WaitForChild("Text")
				if Text2 then
					Text2.Text = Word
				end
			end
		end
		Remote:FireClient(Player, "46T4GkhxgvMFHH9JL8KB", Character)
	end
	table.remove(Words, WordIndex)
end
2 Likes

have you found a proper solution?

now the problem the lopp won’t break.

What do you mean, it will break when the table reaches 0 or there are no more players to loop through

Sorry, I found the error, and then I fixed it.

Thanks, @photonavius @Aanggoodluck @kylerzong @palletenL @Blockzez @Tommybridge

1 Like

The error where you put the “if #Words == 0 then break end” after the “local Word = Words[math.random(1, #Words)]” because the math.random(1, 0) would error before the script would break