Hey, so I am creating an administrator command bar for my game I have been working on. I have the basics of it down but I cannot figure out why it is not working and nothing is showing in output. Does anyone know why?
local message = script.Parent.Text
local prefix=";"
script.Parent.FocusLost:connect(function(EnterPressed)
if EnterPressed then
if message:sub(1,2) == prefix.."kick " then
local Target = game.Players:FindFirstChild(message:sub(2))
if not Target then
print("target not found")
else
Target:Kick("Kicked for a test")
end
end
end
end)
Note: I am not very familiar with command bars or just strings in general.
You’re saving old text in the message variable. Insert the variable inside the FocusLost function so it’s actually getting the most recent text when they loose focus.
Hey, thanks for the fast reply. Is this what it needs to look like?
local prefix=";"
script.Parent.FocusLost:connect(function(EnterPressed)
if EnterPressed then
local message = script.Parent.Text
if message:sub(1,2) == prefix.."kick " then
local Target = game.Players:FindFirstChild(message:sub(2))
if not Target then
print("target not found")
else
Target:Kick("Kicked for a test")
end
end
end
end)
Yeah that would work, for best practice maybe place it inside the next if statement in order to not store variables if the command is not kick, but since you’re probably going to implement more commands this is good.
Finally, It’s something to do with the sub function.
Let’s say that message is “;kick me”
If we perform :sub(1,2) it would return only “;k”
You would have to do :sub(1,5) in order to get “;kick”.
Though, you would have to also change the Target with :sub(6,#message) to get the player, but this is hacky since this also gets everything else after the ;kick, which could not be only the player’s name.
local prefix=";"
script.Parent.FocusLost:connect(function(EnterPressed)
if EnterPressed then
local message = script.Parent.Text
if message:sub(1,5) == prefix.."kick " then
local Target = game.Players:FindFirstChild(message:sub(6,#message))
if not Target then
print("target not found")
else
Target:Kick("Kicked for a test")
end
end
end
end)```
Here’s an improved version since I made a few mistakes.
local prefix=";"
script.Parent.FocusLost:connect(function(EnterPressed)
if EnterPressed then
local message = script.Parent.Text
if message:sub(1,5) == prefix.."kick " then
local Target = game.Players:FindFirstChild(message:sub(7,#message):split(" ")[1])
if not Target then
print("target not found")
else
Target:Kick("Kicked for a test")
end
end
end
end)
Now for the split thing, it’s basically just to get the player and nothing else after the string.
An easier way to handle commands with args would be to just string.split the message and extract the args you need
ex:
local prefix = ";"
local message = ";kick bob Hacking" -- example of script.Parent.Text
local args = message:split(" ") -- "args" will look like this {";kick", "bob", "Hacking"}
local command = table.remove(args, 1) --> ";kick"
-- "args" will now look like this {"bob", "Hacking"}
if command == prefix .. "kick" then
local target = args[1] -- first element of args table, "bob"
local kickReason = args[2] -- second element of args table, "Hacking"
local player = getPlayers(target) -- make a function to find a player from the name you specify
if player then
player:Kick(kickReason)
end
end
If you did everything correctly, then it should kick “bob” with the kick message being “Hacking”
Have your commands in a table. Using many if statements are bad.
Use message:split(" ") to get arguments
Before you do above, convert the string to lowercase with message:lower() so the player can use any type of wording with it.
local commands = {} -- Create an empty table
commands.tp = function(player,arguments) -- If tp was the command it would run this
-- Do code here, while checking if the target (argument 2) exists. (RemoteEvents)
end
local splitString = text:split(' ') -- Split the message
local cmd = splitString[1]:split(prefix) -- Splits the first argument in splitstring with prefix
local command = cmd[2] -- Table would include "/","tp" instead of /tp (if your prefix is /)
if commands[command] then
local arguments = {} -- Making empty table for arguments (player's, values, etc)
for i=2#splitString do -- Getting any arguments after the 2nd argument (after the command)
table.insert(arguments,splitString[i]) -- Insert arguments into table.
end
commands[command](player,arguments) -- Runs your command
end
I assume that this is referring to the gui inside StarterGui. This will infact not work as it is not targeting the replicated version for your client. You would need to do this:
local message = game.Players.LocalPlayer.PlayerGui... -your path
This also explains why you don’t see anything in the bar.
FocusLost won’t activate as it’s not connected to the replicated textbox. He/She would need to go by PlayerGui in order to actually listen to changes made by that client.