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)
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)
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)
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
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.
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.