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

Why is my kill Specific player script not working?!?!?!?

If i say the message in chat nothing happens and no error occurs i tried 2 different versions and both didnt work. Script1 =

game.Players.PlayerAdded:connect(function(player) 
    player.Chatted:connect(function(message)   
    if string.sub(message,1,string.len("/kill "))== "/kill "  then
        if string.sub(message,string.len("/kill"))==player.Name then 
            player.Character.Humanoid.Health = 0
            end
    end
end)
end)


Script2 =

game.Players.PlayerAdded:connect(function(player) 
    player.Chatted:connect(function(message)   
    if message ==("/kill ") then
        if message == ("/kill ") == player.Name then 
            player.Character.Humanoid.Health = 0
            end
    end
end)
end)

1 answer

✓ Accepted answer

Problem: You are trying to compare your message to a player's name which does not exist. The reason for this is because "/kill" is no one's name...well I guess someone might have that somewhere but you are not getting the results you are expecting and that is the reason. You should also use string.sub to help find where the actual player's name should go.

Solution: I will just fix and tweek a few things with looking for your message check with string.sub and actually create another function to find the player.

**Server Script: **

Should be in a server script which is not a local script nor a module script

local prefix = "/" --The prefix that will be used


game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg:sub(1,6) == prefix.."kill " then --The first example you gave this would be correct
			local target = game.Players:FindFirstChild(msg:sub(7)) --There is another way to do this but this is the simplest way, but you need to find the player after the command was used. This is why sub will be used.
			if target then --Check if the player is actually there
				local char = target.Character --Make sure they have a character
				if char then
					char:FindFirstChild("Humanoid").Health = 0
				end
			end
		end
	end)
end)

If you have any problems don't be shy to ask why I would love to help. If this helped you don't forget to up vote and accept the answer.

Here is a link to sub:

http://wiki.roblox.com/index.php?title=Global_namespace/String_manipulation#string.sub

1 comment

0

THX ;)

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