Why does the script run only once, until the player reset the character?

Hello, I have a script that sticks stickers with wounds and scratches on a character if his health level drops below a certain amount, but the problem is that this script only works once, and when the stickers disappear, the character’s health is replenished to the maximum and he takes damage again, this action no longer occurs.

I thought the while true do loop would help me solve this problem, but as it turns out, nothing helped.

Please, who can explain to me what is the matter?

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		local human = character:FindFirstChild("Humanoid")
		while true do
			wait(0.1)
			if human.Health <= 20 then
				local wound = Instance.new("Decal")
				wound.Parent = character.HumanoidRootPart
				wound.Texture = "http://www.roblox.com/asset/?id=166075812"
				wound.Face = "Front"
				wound.Name = "WoundInTorso"
				
				local bf = Instance.new("Decal")
				bf.Parent = character.Head
				bf.Texture = "http://www.roblox.com/asset/?id=1253120824"
				bf.Face = "Front"
				bf.Name = "BloodInFace"
				
				character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed - 13
				
				local tbc = Instance.new("Sound")
				tbc.Parent = character.HumanoidRootPart
				tbc.Volume = 1
				tbc.SoundId = "rbxassetid://664619535"
				tbc:Play()
				
				wait(10)
				
				character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed + 13
				character.HumanoidRootPart.WoundInTorso:Destroy()
				character.Head.BloodInFace:Destroy()
				character.HumanoidRootPart.Tbc:Destroy()
			end
		end
	end)
end)

In other words, I would like to know how to get the above script to run constantly, without having to reset the character.

Put the while wait loop in a local script in starter character script.
Whenever health drops below the threshold, fire a remote event, which a server scripts picks up and then let the server attach the “sticker” to the player.

1 Like

Sorry, but I don’t quite understand what you mean, can you please give an example?

Obviously this is pseudo code

local script

humanoid.HealthChanged:Connect(function(health)  -- screw while wait loops they suck anyway, use healthchanged which fires whenever humanoid health changes
      local Health = Humanoid.Health -- Check player health
      if Health <= 50 then -- if health is less or equal to 50
            RemoteEvent:Fire(Health)  -- Might as well chuck in the health variable to make sanity checks on server side
      elseif Health > 50 then -- health is greater than 50
            RemoteEvent:Fire(Health)
      end
end)

Server script

 -- player variable already known so don't pass it as a parameter when firing the remote on local script
RemoteEvent.OnServerEvent:Connect(function(player, Health) -- When remote event is fired (by client)
      if player.Character.Humanoid.Health == Health then -- Just doing some sanity checks to prevent exploiters from manipulating the remote event
            if Health <= 50 then
                  -- Add stickers to player here
            elseif Health > 50 then
                  -- Remove sticker from player
            end
      end
end)
1 Like