Command script not working, not selecting the correct player

Hello! I’m trying to make a script that heals a player per command. For some reason, my 9. line doesn’t work if v.Name:sub(1, plrName:len()) == plrName then
Full script:

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local prefix = "/heal"
		if msg:sub(1, prefix:len()) == prefix then
			local plrName = msg:sub(prefix:len() + 1 , msg:len())
			print(plrName) --prints out the name, works fine
			for i,v in pairs(game.Players:GetChildren()) do
				--compare plrName to v.Name
				if v.Name:sub(1, plrName:len()) == plrName then
					--It's the right player
					print("yes") --Doesnt get printed
				end
			end
		end
	end)
end)

Thank you!

Is there an error message? If so please provide it.

1 Like

No there isn’t. It works fine until my line 7, the if-statement (if v.Name:sub(1, plrName:len()) == plrName then)

In line 5 local plrName = msg:sub(prefix:len() + 1 , msg:len()), it should have been + 2.

If you look in the console, you would have seen a space in front of the player name, which messes up the search: (It’s small but there)
image

In addition, you should also consider adding a lower(), to bypass capitalization (Quality of life). I have included the lower() function in the code below, however you may remove it as you wish.

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
	    local prefix = "/heal"
	    if msg:sub(1, prefix:len()) == prefix then
		    local plrName = msg:sub(prefix:len() + 2, msg:len())
		    print(plrName) --prints out the name, works fine
		    for i,v in pairs(game.Players:GetChildren()) do
			     if v.Name:lower():sub(1, plrName:len()) == plrName:lower() then
				    --It's the right player
				    print("yes") --Doesnt get printed
			    end
		    end
	    end
    end)
end)
1 Like

There is one more problem though. It does not heal the player. It prints out 100 when doing print(hum.Health) though.

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
	    local prefix = "/heal"
	    if msg:sub(1, prefix:len()) == prefix then
		    local plrName = msg:sub(prefix:len() + 2, msg:len())
		    for i,v in pairs(game.Players:GetChildren()) do
			     if v.Name:lower():sub(1, plrName:len()) == plrName:lower() then
					--v is the correct one!
					local char = v.Character
				 	local hum = char:FindFirstChild("Humanoid")
					hum.Health = 100
					print(hum.Health) --Does print 100				
			    end
		    end
	    end
    end)
end)

Something is wrong with this part

  for i,v in pairs(game.Players:GetChildren()) do
			     if v.Name:lower():sub(1, plrName:len()) == plrName:lower() then
					--v is the correct one!
					local char = v.Character
				 	local hum = char:FindFirstChild("Humanoid")
					hum.Health = 100
					print(hum.Health) --Does print 100				

Thank you!

Also, when printing out char.Name it does print out the full name, so it works fine.

I did game.Workspace.GEILER123456.Humanoid.Health = 50 in the commandbar before, so I did take damage.

This code is working perfectly fine for me. Are you using the command in client mode or server mode? If you are running it in the client mode, go to server mode.

1 Like

In Client Mode, you can’t chat over the Server Mode.

I’ve looked in the workspace under my Humanoid and it says that my health is 100 which is pretty weird. My healthbar still says that it’s 50%.

https://gyazo.com/74dfe44e302ba56a2dcb2979d0d351c1

image

That should say Current: Server, if you want the code to work. If you run it while on the client, the command bar would act as a localscript.

1 Like

Ohh, thank you!! I did run the command over the Client Mode then. ;-;
It works fine now!

I would like to introduce string.split(). What is this? (click)

local Players = game:GetService('Players')
-- If the service isn't created by the time we call it, the GetService() method will create it.

local Prefix = '/'
-- Specified prefix that will be used to execute this command.

Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		local Splits = Message:split(' ') -- string.split is an excellent way of making commands.
		
		if Splits[1]:lower() == Prefix .. 'heal' then
			if Splits[2] and Splits[2]:find('%w') then -- Let's make sure the second argument exists, and includes a valid character.
				for _, v in pairs(Players:GetPlayers()) do
					if v.Name:sub(1, #Splits[2]):lower() == Splits[2]:lower() then
						local TargetCharacter = v.Character
						if TargetCharacter then
							local TargetHumanoid = TargetCharacter:FindFirstChild('Humanoid')
							if TargetHumanoid and TargetHumanoid:GetState() ~= Enum.HumanoidStateType.Dead then -- Let's make sure they're not dead already.
								TargetHumanoid.Health = TargetHumanoid.MaxHealth -- Heal them to their current max health.
								print(v.Name .. ' has been healed by ' .. Player.Name)
							end
						end
					end
				end
			end
		end
		
	end)
end)

-- // Usage: /heal <player> // --

I wrote this in a matter of minutes, but after looking at it, I see no issues that you might fall into.
Hope this helps you out in the future.

1 Like