Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you get the value after the two dots? (Line 7)

Asked by
qVoided 221 Moderation Voter
4 years ago

I am trying to make a kill script with typing :kill (player name here) , but it doesn't work, and it doesn't give me an error.

Here is the code:

local plrs = game:GetService("Players")
local plrName = ""

plrs.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(chr)
        plr.Chatted:Connect(function(message)
            if message == ":kill"..plrName then
                for i,v in pairs(workspace:GetChildren()) do
                    if v.Name == plrName then
                        local hum = workspace[plrName]:WaitForChild("Humanoid")
                        local mh = hum.MaxHealth
                        hum:TakeDamage(mh)
                    end
                end
            end
        end)
    end)
end)

Could anyone fix it? Thanks, I appreciate it.

0
I think add a wait(1) after CharacterAdded:Connect(function(char) to wait for character to load in. Geobloxia 251 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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 --// Kills another player
            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 --// They kill their self.
            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!

0
Thanks! qVoided 221 — 4y
0
No problem! killerbrenden 1537 — 4y
Ad

Answer this question