Chat command system not registering messages

Hello, I was wondering if anyone knows why this isn’t registering the command

My code:

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg:sub(1,6) == Prefix.."vkick " or msg:lower() == Prefix.."vkick  " then
			local Target = game.Players:FindFirstChild(msg:sub(7))
			local Validity = CheckTarget()
			
			if Validity then
				if currentvote ~= tostring(Target) then
					Target = tostring(Target)
					callvote(plr.Name, Target)
					print("Player ", (plr.Name), " has called a vote against", (Target))
				end
			end
		end
	end)
end)

Could you also please show me the CheckTarget() function?

function CheckTarget(Target)
	if not Target then
		warn('Target is not valid')
		return false
	else
		print('Target is valid')
		return true
	end
end
local Validity = CheckTarget(Target)

It still isn’t working, even If I make it print right after the initial chat command

Could you please copy the output and paste it here? It would really help me.

it doesnt even say anything in the output

And may I ask what is the script parented to? I mean what is it’s .Parent?

1 Like

I highly recommend using a debugging method to see which lines of code are running. An easy way to do this is adding print statements after each conditional or at the start of functions.

2 Likes

I could be wrong but I noticed that "vkick " is 6 characters long and when you further add prefix it becomes 7 so the first part of the if statement in the playerChatted event will never be true. Also it appears you call CheckTarget without a target argument so validity is always false.

can you show an example on how you would call the command in game because you could also you string.split to get the target depending on that (the string name of the target)

Yeah, you are right.
So the code should be:

if msg:sub(1,6)+#Prefix == Prefix.."vkick " or msg:lower() == Prefix.."vkick  " then

Hello! I’ve noticed at: msg:lower() == Prefix…"vkick " then There’s 2 spaces after vkick, which probably can break the code!

Generally when I do chat commands I use the string.split(msg, " ") method and it always works for me, so that’s my recommendation, example:

local Prefix = "!"

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		local splits = string.split(msg, " ")
		local Command = splits[1]:lower()
		
		if Command == Prefix.."kick" then
			local PlrToKick = game.Players:FindFirstChild(splits[2])
			if PlrToKick then 
				-- Do something
			end
		end
		
	end)
end)

How it works is: It basically splits the entire message between the spaces and returns them into a table, which makes it really easy to see and separate what the player typed.

Also: You can just change msg:sub(1,6) to msg:sub(1,7) as that way it would catch the Prefix.

2 Likes

so would this kind of thing work?

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local args = string.split(msg," ")
		if args[1]:lower() == Prefix.."vkick" then

			if game.Players:FindFirstChild(args[2]) then
			local Target = game.Players:FindFirstChild(args[2])
			
				if Validity then
					if currentvote ~= tostring(Target) then
						Target = tostring(Target)
						callvote(plr.Name, Target)
						print("Player ", (plr.Name), " has called a vote against", (Target))
					end
				end
			end
		end
	end)
end)

its basically just what the person above did mixed with your script

Yeah, that would probably kinda work! You would have to edit it of course, so it suits the gameplay and the purpose of the script, which may be unknown to us.

You forgot to add local Validity = CheckTarget(Target).

oh right

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local args = string.split(msg," ")
		if args[1]:lower() == Prefix.."vkick" then

			if game.Players:FindFirstChild(args[2]) then
			local Target = game.Players:FindFirstChild(args[2])
                 -- basically all Validity did was check if the target existed which is what the line 2 above this does
				if currentvote ~= tostring(Target) then
					Target = tostring(Target)
					callvote(plr.Name, Target)
					print("Player ", (plr.Name), " has called a vote against", (Target))
				end
			end
		end
	end)
end)

but technically i didnt need the if validity because the if statement already does that enitre function, if you need it for later you could add a variable like this


game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local args = string.split(msg," ")
		if args[1]:lower() == Prefix.."vkick" then
			local Target = game.Players:FindFirstChild(args[2])
			local Validity = CheckTarget(Target) 
			if Validity == true then
				if currentvote ~= tostring(Target) then
					Target = tostring(Target)
					callvote(plr.Name, Target)
					print("Player ", (plr.Name), " has called a vote against", (Target))
				end
			end
		end
	end)
end)

though in the first script all they did was check for validity and not if it was true or false so i went ahead and added a true to it so it actually does something instead of continuing even if it doesn’t exist

Thanks so much for the help! This is the solution