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)
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.
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)
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.
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).
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