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

Script to change "Health" and "MaxHealth" using "onChatted" not working?

Hi, I've made a script to change a players Health and/or Max health when they chat. I tried to make a prefix for it, and that may be a problem as this is my first time attempting to make a prefix.

Here is the Script:

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg) 
	if msg == "MaxHealth" .. tonumber(msg) then
		player.Character.Humanoid.MaxHealth = tonumber(msg)
	end	
	if msg == "Health" .. tonumber(msg) then
		player.Character.Humanoid.Health = tonumber(msg)
	end
	end)
end)

Thanks.

1 answer

✓ Accepted answer

Here's what you want to do:

game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg)
		-- If the message starts with "maxhealth " then
		-- It's good to use string.lower so it accepts all these:
		-- "MaxHealth ", "maxhealth ", "MAXHEALTH ", etc
		if (string.lower(string.sub(msg, 1, 10)) == "maxhealth ") then
			-- If there's a number after the "maxhealth " then
			-- Set the player's max health to that
			local number = tonumber(string.sub(msg, 11))
			if (number ~= nil) then
				player.Character.Humanoid.MaxHealth = number
			end
		-- Same but with health
		elseif (string.lower(string.sub(msg, 1, 7)) == "health ") then	
			local number = tonumber(string.sub(msg, 8))
			if (number ~= nil) then
				player.Character.Humanoid.Health = number
			end
		end
	end)
end)

2 comments

0

Thanks, this worked perfectly, Would it also work if I added a "=" after the "health", for example if I edited the code so it said "if (string.lower(string.sub(msg, 1, 11)) == "maxhealth = ") would it work or does the "=" at the end change the Strong?

0

string.sub(msg, 1, 11) gives you the first 11 characters of your message, and "maxhealth = " has 12 characters (counting spaces), so if you wanted it to work you should change that 11 to a 12 and it should work! But also keep in mind that the number starts at the 13th character, so you'd also have to change number = tonumber(string.sub(msg, 13))

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