Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

1 comment

0

I think add a wait(1) after CharacterAdded:Connect(function(char) to wait for character to load in.

1 answer

✓ 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 --// 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!

2 comments

0

Thanks!

0

No problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.