Touched function doesn't work properly a second time

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    When player touches part, their hp is set to 1
  2. What is the issue? Include screenshots / videos if possible!
    It only works once
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried to print every time the part is touched by the player to see if its something wrong with the function, added a cooldown/debounce. Function prints the text but doesn’t set hp to 1, debounce does nothing.
script.Parent.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hit.Parent:FindFirstChild("Humanoid") then
		hum.Health = 1
		print("Hp set to 1")
	end
end)
1 Like

From what I understand you are trying to set the player hp to 1 when he touches a part.

Here is a working script:

script.Parent.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	
	if hum then
		hum.Health = 1
		
		print("Set".. hit.Parent.Name.. "health to 1")
	end
end)
1 Like

It does the same thing my script did. Prints things but doesnt set the hp to 1 if you touch the part a second time.

Is it because you don’t find the health bar ? For me the health bar was moved on the left side of the screen

1 Like

I can see the hp bar… and it’s not going back to 1 hp

Looking at the script, there’s nothing wrong with it. I think it’s something to do with you, as that script should work flawlessly.

Try making a GUI showing the character hp, maybe it will help you ?

Add a print to check if the .Touched event is firing. Nvm, you already have that.

maybe its because the hp is already 1?

I am healing with a script then trying to touch it…

is the healing script a server script?

Show us that script too, please.

It is a local script inside starter player

So basically, when a value changes on the client, the server does not see it, therefore it cannot change it.

local player = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local character = player.LocalPlayer.Character 
local Humanoid = character:WaitForChild('Humanoid')
local REGEN_RATE = 50/100
local REGEN_STEP = 0.1
local particula = ReplicatedStorage.RCTParticle

particula:Clone()
particula.Enabled = false

UserInputService.InputBegan:Connect(function(Input, gameProcessed) 
	if Input.KeyCode == Enum.KeyCode.M then
		while UserInputService:IsKeyDown(Enum.KeyCode.M) do
			local dt = wait(REGEN_STEP)
			local dh = dt*REGEN_RATE*Humanoid.MaxHealth
			print("M held")
			particula:Emit(25)
			--wait(particula.Lifetime.Max)
			Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)	
			wait(0.1)
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input, gameProcessed)
	if Input.KeyCode == Enum.KeyCode.M then
		wait(particula.Lifetime.Max)
		print("M let go")
		particula.Enabled = false
		particula:Clear()
	end
end)

Try the script that make the player health to 1 hp without this localscript enabled

I would recommend looking on the server side if it sees the health regen.

1 Like

Still does not work. Maybe it has something to do with how I removed the default roblox healing script? I made a new script in starter player called health and its empty so the character doesn’t heal naturaly

That might be it, I suggest trying the script in a new template to see if it’s the real problem

was the value changed on the server side?