Search code examples
variablesparametersluaroblox

Detect parameters in a ROBLOX player's message? (LUA)


I'm trying to make a simple 'kill command' in Roblox Studio where you type ";kill player" and it will kill them.

The part I am struggling with is how on earth to separate the words before and after the space, and store them as variables?

Steps I need in my code:

  1. Check if the message deliverer's name is equal to the value of 'Owner'
  2. Check if the message contains ';' at the beginning (the prefix)
  3. Check if the message contains one space
  4. If all of the above are true, Then set the word before the space to the variable,'cmd' and set the word after the space to the variable, 'username'.

The rest I can figure out on my own.

Here's what my code is currently:

local Owner = "Djraco"
local Prefix = ";"


game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)
        
         --<code goes here>

    end)

end)

Thanks in advance!


Solution

  • local Owner = "Djraco"
    local Prefix = ";"
    
    
    game.Players.PlayerAdded:Connect(function(player)
        player.Chatted:Connect(function(msg)
            
             local msg = msg:split(" ")  -- Seperate your message every space
             if player.Name == Owner and msg[1] == "kill" and msg[2] ~= nil then game:GetService("Players")[msg[2]].Character.Humanoid.Health = 0 end
             
    
        end)
    
    end)
    

    The above should work. Let me know if you need more help.