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

Why doesn't my custom command work?

I am trying to make a command, so if you say [kill littelbigblox] it kills littelbigblox. The error is that Child is not a Child of Players. How would i fix this?

player.Chatted:connect(function(message)
	if message == "[kill "..game.Players.Child.."]" then --Kill Command
		local name = string.sub(message,1,#message - 1)	
		game.Players:FindFirstChild(name).Character.Head:Destroy()	
	end
end)

2 answers

✓ Accepted answer

The Chatted event is useful for many things, such as making commands. However, your script cannot do anything with game.Players.Child because it is nonexistent. To correctly find players, you need to use a function to iterate through the Players.GetPlayers() list.

--Credit where credit is due
	--This script is found on the ROBLOX wiki, cited below.

function findPlayer(name)		--This function iterates through each player.
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end

Chat Commands from the wiki. Use this link to refine your code.

player.Chatted:connect(function(message)
	if message == string.sub("[kill ",1,6) then
		local name = string.sub(message,7,#message - 1)
		for _, player in pairs(game.Players:GetPlayers()) do
       			 if player.Name:lower() == name:lower() then
            			game.Players.FindFirstChild(name).Character.Head:Destroy()
       		 	end
    		end
	end
end)

2 comments

0

It doesnt work if i say [kill littelbigblox]

0

Ack... I'd just replicate the code on the ROBLOX wiki (that i posted above) and edit it so it's your own command.

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