✓ Accepted answer
You want to use String:Sub(beginning, end)
As this will automatically get the rest of the sentence.
To do this
So, for example. If I wanted to get the rest of the name that a player said I would do something like this.
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if message:sub(1,6) == ":kill " then
local targetPlayer = message:sub(7)
print(targetPlayer)
end
end)
end)
If I said :kill killerbrenden this will print killerbrenden.
The sub(1,6) is the length of the message :kill , sub also counts spaces and other characters.
So, for your code it would look something like this.
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if message:sub(1,6) == ":kill " then
if game:GetService("Players"):FindFirstChild(message:sub(7)) then
local targetPlayer = game:GetService("Players"):FindFirstChild(message:sub(7))
local targetCharacter = targetPlayer.Character or targetPlayer.CharacterAdded:Wait()
if targetCharacter:FindFirstChild("Humanoid") then
local targHum = targetCharacter:FindFirstChild("Humanoid")
targHum:TakeDamage(targHum.MaxHealth)
end
end
elseif message:sub(1,5) == ":kill" then
local targetPlayer = player
local targetCharacter = player.Character or player.CharacterAdded:Wait()
if targetCharacter:FindFirstChild("Humanoid") then
local targHum = targetCharacter:FindFirstChild("Humanoid")
targHum:TakeDamage(targHum.MaxHealth)
end
end
end)
end)
The second part is if the player says :kill then they will die instead of another player, but if they say :kill Player_Name then that targeted player will die instead of the speaker.
Hope this helped! If this worked, don't forget to select this as the answer!
1 comment
I think add a wait(1) after CharacterAdded:Connect(function(char) to wait for character to load in.