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

I am not to sure that table.insert is working or being used correctly?

I am trying to make certain things happen when certain people say something and one of these things is to add a players name into a table. That player in the table will be able to do something when they say something. The thing is I am unsure if I am doing the table.insert right?

local admins = {["Player"] = true,["Player1"] = true,["yougottols1"] = true}
local insert = {[""] = true,[""] = true,[""] = true,[""] = true,[""] = true }


game.Players.PlayerAdded:connect(function(plr)
	plr.Chatted:connect(function(msg)
		for i,v in pairs(admins, insert) do
			if admins[plr.Name] and true then
				if msg:lower() == "/Risen" then
				print("Sucess")	
				elseif 
					 string.match(msg, "/add") then
					local play = game.Players:GetPlayers()
					for i = 1, #play do
					if string.match(msg, string.lower(play[i].Name)) then
						local p = game.Players:FindFirstChild(play[i].Name)
						table.insert(insert, 1, p)
						print("YES")
					end
		
		end
		       end
			end
		end
	end)
end)

2 answers

✓ Accepted answer
local p = game.Players:FindFirstChild(play[i].Name)
table.insert(insert, 1, p)

This is the section of the code which is incorrect. The format of table.insert is: table.insert (table, [pos,] value), as the wiki exactly says. Both of the tables are dictionaries NOT arrays, this means they use string indexes instead of the usual integer values.

The table.insert function is supposed to insert the value to the end of an array or to the point specified by the 2nd argument if three arguments are present. There is no index of 1, or any number actually, so this won't work. The way to set values in a dictionary is the usual table[key] = value. So that's how you'd set the value instead. Dictionaries are the easier type of table to use, if you use them properly.

So the code I quoted should be this:

insert[play[i].Name] = true -- p is actually equal to play[i] anyway, go figure!

Other things I've noticed:

  • Pairs only takes one table as an argument, not two, so this could lack its intended functionality.

  • string.match is intended for patterns like %d+, string.find is much easier and simpler to use (plus it makes sense).

  • "admins[plr.Name] and true" was probably meant to be admins[plr.Name]==true, so I removed "and true" though you can add it back if you need an extra condition.

  • You could've used the pairs function for iterating through the GetPlayers table, which is more efficient than using a plain for loop (unless you were planning on ordering them based on a factor of some kind).

Here's your code fixed up for you, you're very welcome :) Let me know if anything's up, I'm very likely to make a mistake as I'm not checking this in Studio (sorry if there are any indentation issues when pasting into studio). Cheers :D - CD

local admins = {["Player"] = true,["Player1"] = true,["yougottols1"] = true}
local insert = {} -- No point having values yet, it's fine as blank initially.

local commands = { -- thought this might make it easier to add commands?
	["/Risen"] = function(message)
		print("Success") 
	end,
	["/add"] = function(message)
		for _,Player in pairs(game.Players:GetPlayers()) do
       		if message:find(Player.Name) then
				insert[Player.Name] = true
                print("YES")
            end
       	end
	end,
}

function startsWith(message,starts,forceCaps)
	return (forceCaps and message:sub(1,starts:len())==starts or message:lower():sub(1,starts:len())==starts:lower())
end

game.Players.PlayerAdded:connect(function(plr)
	plr.Chatted:connect(function(msg)
		local function Check(tab,rank)
			-- I added a "rank" parameter, this allows you distinguise between levels of admin
			-- e.g. Owner / Admin / Temp-Admin etc
			for i,v in pairs(tab) do
				if tab[plr.Name]~=nil then -- the "and true" came from here, if that means anything
					for index,command in pairs(commands) do
						if startsWith(msg,index) then
							command(msg)
							return true
						end
					end
				end
			end
		end
		for index,adminTable in pairs({admins,insert}) do
			if Check(adminTable,index) then
				break -- don't run the admin code twice if the user is in more than one of the tables!
			end
		end
	end)
end)

(Sorry if I edited your code too much, just trying to help you as much as possible!)

2 comments

0

Woah, thank you so much!!!!!!! I will study it to not make the mistake next time, thank you!

0

You're welcome, happy to help. :D

ROBLOX itself is using the '/' for commands try using something like a ';' or a symbol for your commands ;) Hope that fixes da script

2 comments

0

That is more of a personal prefix command and i just am trying to see if i am starting to use the table.insert correctly.

0

The prefix doesn't really matter, the Chatted event SHOULD fire for any chatted message.

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