A small issue with string.sub() when it comes to getting the third argument

#str works for strings as well as tables.

1 Like

There is no problem with the code, I’m just not sure how to get a third argument.

It’s possible you’re looking for string.split, it’s at the bottom of the page I linked. The way it works (and why it will be useful to you) is it will split a string based on whatever separator you pass in, and put all of the substrings into a table:

table string.split ( string s, string separator = , )

Instead of using string.sub, I’d highly suggest you use the recently added string.splt to get a more concise table of arguments.

game:GetService('Players').PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		local splits = string.split(msg," ")
		if splits[1] == "!report" then
			 local player
			 local reason
			 for i,v in next, game:GetService('Players'):GetPlayers() do
				if string.sub(v.Name,1,#splits[2]):lower() == splits[2]:lower() then
					player = v
					table.remove(splits,2)
					table.remove(splits,1)
					reason = table.concat(splits," ")
				end
			end
			print(player, '|',reason)
		end
	end)
end)


(updated console log below)
image

3 Likes

I see, but a third argument? 11111111

Working on that right now, actually forgot to join the arguments! Will reply when edited.

Okie, I appreciate your help. ^.^

1 Like

I don’t understand your motives of spoonfeeding @Syclya code that they should be able to figure out how to write themselves easily enough. Spoonfeeding is not a good method of teaching, and you should try to refrain from doing it.

1 Like

Seeing the solution can also help the others to learn by looking at it and to understand it. There are 100 ways to teach & learn in my opinion.

I haven’t really looked into string manipulation that much, so hopefully a solution to this might get me started.

There may be 100 ways to teach and learn, but spoonfeeding code is not one. You learn nothing from getting spoonfed code, and frankly, you should already have your solution based on prior posts. If you’d like to speak with me more about this, I suggest Direct Messaging me.

1 Like

I’ve edited my post to show what it should look like using string.split.
Feel free to mark it as the solution if you wish!

In response to you, spoonfeeding is one of the many ways someone learns. You are allowed to input your opinion that spoonfeeding should not be used to teach, but as bolded it is your opinion. I myself learned most of what I can do because I had people able to help me, people that were more knowledgeable than me in the field and were enlightened to help me become better so that I can, in turn, help others when needed. Thanks for coming to my ted talk.

1 Like

I really appreciated your help. Thank you so much ^.^

1 Like

Apparently:
[ServerScriptService.Report tracker:31: attempt to concatenate local ‘player’ (a userdata value)

Script:

local Data = {
				["content"] = "**Player** " .. player .. " has been reported by **" .. Player.Name .. " with reason: **" .. reason .. "**"
			}

It should be player.Name, not the actual player object.

No, because this works:
print(player, ‘|’,reason)

The print function automatically tostrings every value, otherwise it’s impossible to display (in a readable format, I mean). tostring(player) will be equivalent to player.Name.

1 Like

:facepalm:

I’ve been scripting for a few years, but that was honestly new knowledge for me. Haven’t messed around much with print() unless when it comes to debugging. Thank you! ^^

1 Like

Just another small issue I can’t seem to fix:

game:GetService('Players').PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		local splits = string.split(msg, " ")
		if splits[1] == "!report" then
			 --if Cooldown[Player.UserId] then
				--Event:FireClient(Player, "", "", "COOLDOWN")
				--return
			 --end
			 local player
			 local reason
			 for i,v in pairs(game:GetService('Players'):GetPlayers()) do
				if string.sub(v.Name, 1, #splits[2]):lower() == splits[2]:lower() then
					player = v
					table.remove(splits, 2)
					table.remove(splits, 1)
					reason = table.concat(splits, " ")
				end
			end
			
			--print(player, '|',reason)
			if tostring(player) == Player.Name then
				Event:FireClient(Player, "", "", "SELF_REPORT")
				return
			end
			if not player then
				return
			end
			if not reason then
				return
			end
			
			local Data = {
				["content"] = "Player **" .. tostring(player) .. "** has been reported by **" .. Player.Name .. "** with reason: ``` " .. tostring(reason) .. "```"
			}
			Data = http:JSONEncode(Data)
			http:PostAsync("not showing :p", Data)
			Cooldown[Player.UserId] = Player.UserId
			Event:FireClient(Player, player, reason, "SUCCESS")
			wait(60)
			Cooldown[Player.UserId] = nil
		end
	end)
end)

When only typying !report, this happens and I can’t see why:
[ServerScriptService.Report tracker:26: attempt to get length of field ‘?’ (a nil value)

This is because there’s only one member of splits. When you do #splits[2], it’s checking the length of splits[2]. When you just type !report, there’s not going to be a splits[2]. You can fix this by checking if splits[2] and splits[3] exist before checking their length. For example, you could replace if splits[1] == "!report" then with if splits[1] == "!report" and splits[2] and splits[3] then

2 Likes

Noticed that. But the script seems to have new errors everytime I test it:

game:GetService('Players').PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		local splits = string.split(msg, " ")
		if splits[1] == "!report" and splits[2] and splits[3] then
			if Cooldown[Player.UserId] then
				Event:FireClient(Player, "", "", "COOLDOWN")
				return
			end
					
					local player
					local reason
					for i, v in pairs(game:GetService('Players'):GetPlayers()) do
						if string.sub(v.Name, 1, #splits[2]):lower() == splits[2]:lower() then
							player = v
							table.remove(splits, 2)
							table.remove(splits, 1)
							reason = table.concat(splits, " ")
						end
					end
			
					--print(player, '|',reason)
					if tostring(player) == Player.Name then
						Event:FireClient(Player, "", "", "SELF_REPORT")
						return
					end
					
					if player == nil then
						Event:FireClient(Player, "", "", "NO_PLAYER")
						return
					end
					
					if reason == nil then
						return
					end
					
					local Data = {
						["content"] = "Player **" .. tostring(player) .. "** has been reported by **" .. Player.Name .. "** with reason: ``` " .. tostring(reason) .. "``` @everyone"
					}
					Data = http:JSONEncode(Data)
					http:PostAsync("", Data)
					Cooldown[Player.UserId] = Player.UserId
					Event:FireClient(Player, tostring(player), tostring(reason), "SUCCESS")
					wait(60)
					Cooldown[Player.UserId] = nil
		end
	end)
end)

^ when you try to report yourself.