Can I make code shorter?

Hi, developers!
I made a script that increases max health when player buy the pass or join the group.
I tested and it worked! But I want to know can make my code shorter.
Thanks in advance!
code:

local mps = game:GetService("MarketplaceService")
local id = 27956029
local groupId = 6589440
game.Players.PlayerAdded:Connect(function(player)
	if mps:UserOwnsGamePassAsync(player.UserId, id) then
		local character = player.Character
		local Humanoid = character:WaitForChild("Humanoid")
		if Humanoid then
			if player:IsInGroup(groupId) then
				Humanoid.MaxHealth += 20
				task.wait()
			end
			Humanoid.MaxHealth += 100
			Humanoid.Health = Humanoid.MaxHealth
		end
		player.CharacterAdded:Connect(function(character)
			local Humanoid = character:WaitForChild("Humanoid")
			if Humanoid then
				if player:IsInGroup(groupId) then
					Humanoid.MaxHealth += 20
					task.wait()
				end
				Humanoid.MaxHealth += 100
				Humanoid.Health = Humanoid.MaxHealth
			end
		end)
	else
		if player:IsInGroup(groupId) then
			local character = player.Character
			local Humanoid = character:WaitForChild("Humanoid")
			
			if Humanoid then
				Humanoid.MaxHealth += 20
				Humanoid.Health = Humanoid.MaxHealth
			end
			player.CharacterAdded:Connect(function(character)
				local Humanoid = character:WaitForChild("Humanoid")
				if Humanoid then
					Humanoid.MaxHealth += 20
					Humanoid.Health = Humanoid.MaxHealth
				end
			end)
		end
	end
end)

mps.PromptGamePassPurchaseFinished:Connect(function(player, assetId, purchased)
	local character = player.Character
	local Humanoid = character:WaitForChild("Humanoid")
	if Humanoid then
		if player:IsInGroup(groupId) then
			Humanoid.MaxHealth += 20
			task.wait()
		end
		Humanoid.MaxHealth += 100
		Humanoid.Health = Humanoid.MaxHealth
	end
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:WaitForChild("Humanoid")
		if Humanoid then
			if player:IsInGroup(groupId) then
				Humanoid.MaxHealth += 20
				task.wait()
			end
			Humanoid.MaxHealth += 100
			Humanoid.Health = Humanoid.MaxHealth
		end
	end)
end)
3 Likes

Use seperate scripts for them. Probably will and can shorten the script :wink:

You’re using player.CharacterAdded event several times, why not use it once?

1 Like

Thank you so much, I’ll change it! :smiley:

2 Likes