Script not adding to table? (I'm not sure what is happening?)

I’ll cut straight to the chase. So basically what I think is happening is that the script doesn’t properly insert the items into a table. I’m trying to make admin commands following AlvinBlox’s video(modified), and it works fine for him, so can anyone help me here?

Server script:

local adminCommands = require(script.Commands)
local prefix = ":"


game.Players.PlayerAdded:Connect(function(p)
	p.Chatted:Connect(function(msg, receiver)
		if receiver then return end 
		if p:WaitForChild("HasAdmin").Value == false then return end
		--print("ok")
		local splitMSG = msg:split(" ")
		if splitMSG[1]:sub(1, 1) == prefix then
			local cmdprefixed = splitMSG[1]
			local cmd = cmdprefixed:sub(2, #splitMSG[1])
			local playersToActOn = adminCommands:getPlayers(p, splitMSG[2])
			splitMSG[2] = playersToActOn
			--print("fine")
			if adminCommands[cmd] then
				--print("yeS")
				local argumentsToSend = {playersToActOn}
				--print("no")
				
				for i = 2, #splitMSG, 1 do
					table.insert(argumentsToSend, splitMSG[i])
				end
				--print("almost")
				
				adminCommands[cmd](p, argumentsToSend)
				--print("yes")
			end
		end
	end)
end)

Module script:

local commands = {}

function findPlayer(player)
	for i, playr in pairs(game.Players:GetPlayers()) do
		if playr.Name:lower():sub(1, #player) == player then
			return playr
		end
	end
	return nil
end

function commands:getPlayers(playersent, whichplayers)
	if whichplayers == "me" then
		return playersent
	end
	local players = {}
	for i, p in pairs(game.Players:GetPlayers()) do
		if whichplayers == "others" then
			if p ~= playersent then
				table.insert(players, p)
			end
		end
		if whichplayers == "all" then
			table.insert(players, p)
		end
	end
	return players
end

commands.speed = function(arguments)
	if type(arguments[1]) == "table" then -- where i get error
		for i, p in pairs(arguments[1]) do
			if findPlayer(p) then
				workspace[p].Humanoid.WalkSpeed = arguments[2]
			end
		end
	else
		if findPlayer(arguments[1]) then
			workspace[arguments[1]].Humanoid.WalkSpeed = arguments[2]
		end
	end
end

return commands

This is the error I get as well.
image

It seems that GetPlayers can return a singular player value. The error is caused by you sending a player as “arguments”, instead of a table.

Inside your .speed function, instead of putting “arguments[1]” inside type and indexing only the first value, just put “arguments”.

Also, change “type” to “typeof”. “type” doesn’t help you find out what type of data a variable is @FirewolfYT_751Adult.

This happened before I added that function as well.

No errors, but doesn’t work at all.

Also, change “type” to “typeof”. “type” doesn’t help you find out what type of data a variable is.

Still does not work. 30 charsss

You are sending the player as the first argument, and “arguments” as the second. Make sure to add the player as the first parameter.

commands.speed = function(player,arguments)

Now I get this issue.
image
Happens in this function

function findPlayer(player)
	for i, playr in pairs(game.Players:GetPlayers()) do
		if playr.Name:lower():sub(1, #player) == player then
			return playr
		end
	end
	return nil
end

Called here.

commands.speed = function(plr, arguments)
	if type(arguments) == "table" then
		for i, p in pairs(arguments) do
			if findPlayer(p) then
				workspace[p].Humanoid.WalkSpeed = arguments[2]
			end
		end
	else
		if findPlayer(plr) then
			workspace[plr].Humanoid.WalkSpeed = arguments[2]
		end
	end
end

I tried player.Name and that doesn’t work either.

Can’t you just shorten that function to this, instead of doing all of that string stuff?

function findPlayer(player)
	for i, playr in pairs(game.Players:GetPlayers()) do
		if playr.Name == player.Name then
			return playr
		end
	end
	return nil
end

Also, try printing plr in your speed function, to see if it returns nil.

Well, I edited it so that the message the player types in gets turned into lowercase, and I want to accept partial names as well.

1 Like

Also, try printing plr in your speed function, to see if it returns nil.

Interesting. It printed my username, and then returned nil. So it does get my username, and then it becomes nil?

1 Like

Where is it printing your username?

function findPlayer(player)
	print(player.Name)
	for i, playr in pairs(game.Players:GetPlayers()) do
		if playr.Name:lower():sub(1, #player.Name) == player.Name then
			return playr
		end
	end
	return nil
end

image

So it seems that findPlayer is not working properly, and is returning nil when it shouldn’t.

What do you assume is the cause of it returning nil?

I believe something is wrong with the logic of the string parser thing. Try simplifying it as I demonstrated in the previous post, to just comparing the player names. I don’t understand why you are doing it this way anyway, since it is just working with names from game.Players, and not anything chat related.

But then how would I get it to work it i typed in “fire” instead of my full username, and no one else is named fire?

Try this function instead:

local function findPlayer(stringg) -- stringg is the chat message you want to check
    for _, v in pairs(Players:GetPlayers()) do
         if stringg:lower() == (v.Name:lower()):sub(1, #stringg) then
               return v
         end
    end
end

This would check to see if a player’s name equals the string, and if so, return the player.

1 Like

Ok, so it finally does sort of work. When I type in “me”, I speed up. However, typing in an actual player’s username or a part of it does this.
image
This is what I have (I also edited the server script a tiny bit):

local commands = {}

function findPlayer(stringg)
	for _, v in pairs(game.Players:GetPlayers()) do
         if stringg:lower() == (v.Name:lower()):sub(1, #stringg) then
               return v
         end
    end
end

function commands:getPlayers(playersent, whichplayers)
	if whichplayers == "me" then
		return playersent
	end
	local players = {}
	for i, p in pairs(game.Players:GetPlayers()) do
		if whichplayers == "others" then
			if p ~= playersent then
				table.insert(players, p)
			end
		end
		if whichplayers == "all" then
			table.insert(players, p)
		end
	end
	if whichplayers ~= "me" or whichplayers ~= "others" or whichplayers ~= "all" then
		return whichplayers
	end
	return players
end


commands.speed = function(arguments)
	if type(arguments[1]) == "table" then
		for i, p in pairs(arguments) do
			if findPlayer(p.Name) then
				workspace[p.Name].Humanoid.WalkSpeed = arguments[2]
			end
		end
	else
		if findPlayer(arguments[1].Name) then
			workspace[arguments[1].Name:sub(1, #arguments[1].Name):lower()].Humanoid.WalkSpeed = arguments[2]
		end
	end
end

return commands