Script doesnt make a new instance of proximity prompt

As far as the title goes I have made this incapacitation script where I wanna add a revive feature but for some reason it doesnt add a new instance of a proximity prompt. Any ideas?

btw my code might be very messy

local onPlayerLoaded = function(player)
	local onCharacterLoaded = function(Char)
		local humanoid = Char:WaitForChild("Humanoid")
		local hrp = Char:WaitForChild("HumanoidRootPart")
		wait(2)
		local anim = humanoid:LoadAnimation(incap)
		local anim2 = humanoid:LoadAnimation(recovered)
		
		--part 1
		
		humanoid.HealthChanged:Connect(function()
			if humanoid.Health <= 50 then
				print("player incapacitated")
				
				local uhsclone = incapuhs:Clone()
				uhsclone.Parent = player.PlayerGui
				
				anim:Play()
				
				hrp.Anchored = true
				task.wait(5)
				hrp.Anchored = false
				
				wait(2.5)
				
				uhsclone:Destroy()
				while humanoid.Health <= 69 do
					wait(2)
					humanoid:UnequipTools()
				end	
				
			elseif humanoid.Health >= 70 then 
				print("player is recovering")				
				hrp.Anchored = false
				humanoid.WalkSpeed = 5				
				if humanoid.Health == 100 then
					print("player fully recovered or healthy")
					humanoid.WalkSpeed = 16
					anim:Stop()				
				end
				if humanoid.Health <= 70 then
					wait(1)
					local proxPrompt = Instance.new("ProximityPrompt")
					player.CharacterAdded:Wait() 
					proxPrompt.Parent = player.Character:WaitForChild("Torso")
					proxPrompt.RequiresLineOfSight = false
					proxPrompt.Triggered:Connect(function()
						-- do stuff here
					end)
				end
			end;
		end);
	end;

	player.CharacterAdded:Connect(onCharacterLoaded);
	if(player.Character) then onCharacterLoaded(player.Character) end;
end;

game.Players.PlayerAdded:Connect(onPlayerLoaded);
for _, p in pairs(game:GetService("Players"):GetPlayers()) do
	onPlayerLoaded(p);
end;
if humanoid.Health <= 70 then
					wait(1)
					local proxPrompt = Instance.new("ProximityPrompt")
					player.CharacterAdded:Wait() 
					proxPrompt.Parent = player.Character:WaitForChild("Torso")
					proxPrompt.RequiresLineOfSight = false
					proxPrompt.Triggered:Connect(function()
						-- do stuff here
					end)

It’s because you put the bottom snippit inside of the if humanoid.Health >= 70
while it only runs when humanoid.Health <= 70
because of that, it’ll only create the proximityprompt when your health is exactly 70.

So I should make it a different number. Or will it still make it so it’ll only work when my health is exactly that number.

You want the proximity prompt to only appear when your incapacitated right?
If so, you’ll want to move it over into the other half of the if statement, where you do incapacitate the character.

I’ve attempted this but it didn’t work?
Can you show what I’ve done wrong here?

		humanoid.HealthChanged:Connect(function()
			if humanoid.Health <= 50 then
				print("player incapacitated")
				
				local uhsclone = incapuhs:Clone()
				uhsclone.Parent = player.PlayerGui
				
				anim:Play()
				
				hrp.Anchored = true
				task.wait(5)
				hrp.Anchored = false
				
				wait(2.5)
				
				uhsclone:Destroy()
				
				local proxPrompt = Instance.new("ProximityPrompt")
				player.CharacterAdded:Wait() 
				proxPrompt.Parent = player.Character:WaitForChild("Torso")
				proxPrompt.RequiresLineOfSight = false
				proxPrompt.Triggered:Connect(function()
					-- do stuff here
				end)
				
				while humanoid.Health <= 69 do
					wait(2)
					humanoid:UnequipTools()
				end	
				
			elseif humanoid.Health >= 70 then 
				print("player is recovering")				
				hrp.Anchored = false
				humanoid.WalkSpeed = 5		
				if humanoid.Health == 100 then
					print("player fully recovered or healthy")
					humanoid.WalkSpeed = 16
					anim:Stop()				
				end
			end;
		end);
	end;

	player.CharacterAdded:Connect(onCharacterLoaded);
	if(player.Character) then onCharacterLoaded(player.Character) end;
end;

game.Players.PlayerAdded:Connect(onPlayerLoaded);
for _, p in pairs(game:GetService("Players"):GetPlayers()) do
	onPlayerLoaded(p);
end;

What’s breaking exactly? Is it still not making a proximity prompt when it prints “player incapacitated”?
Also I should note that, it looks like every time the humanoids health changes while below 50hp it’ll play the whole incapacitation sequence again.

Yea it does and i haven’t found a solution to that yet but im mainly focused on the reviving system atm.

And yes it still doesn’t make a new instance of it when it prints.

Try doing
print(proxPrompt.Parent) after you’ve created it (also, check to see if the instance is actually under the characters torso in the explorer while the game is running)
Additionally, you can fix it running multiple times by doing something like a debounce. Where you set the debounce to true when you incapacitate the character, and set it to false when they wake back up. And just check to see if the debounce is already true when you go to incapacitate.

It doesn’t seem to print anything. I also put it right under where it gets parented to the players head.

Where is this script positioned in?

Its inside Serverscriptservice

This is a sign that it never reaches that part of the script, and something is causing it to stall indefinitely.
Place prints throughout that whole section and find out where the prints stop.

I mean its able to reach this part
image

That’s because that part is only run if the other part isn’t ran.

if humanoid.Health <= 50 then
    --This part will run
elseif humanoid.Health >= 70 then
    --OR this part will run
end

The two parts don’t impact eachother.
You need to place prints throughout the first part and find out where it breaks.

		
		humanoid.HealthChanged:Connect(function()
			if humanoid.Health <= 50 then
				print("player incapacitated")
				
				local uhsclone = incapuhs:Clone()
				uhsclone.Parent = player.PlayerGui
				
				anim:Play()
				
				hrp.Anchored = true
				task.wait(5)
				hrp.Anchored = false
				
				wait(2.5)
				
				uhsclone:Destroy()
				
				print("test worked 1")
				
				
				local proxPrompt = Instance.new("ProximityPrompt")
				print("test worked 2 made proximity")
				player.CharacterAdded:Wait() 
				proxPrompt.Parent = player.Character:WaitForChild("Torso")
				print(proxPrompt.Parent)
				proxPrompt.RequiresLineOfSight = false
				proxPrompt.Triggered:Connect(function()
					-- do stuff here
				end)
				print("test worked 3 worked")
				while humanoid.Health <= 69 do
					wait(2)
					humanoid:UnequipTools()
				end	

After placing these down it seems the 3rd print doesn’t work…

This appears to be the culprit.
This makes it wait until your character dies and respawns again.

1 Like

Hm. It doesn’t seem to get rid the proximityprompt once revived. I know this is solved already but do you know how to fix it?

Sorry I didn’t check my devforum tab for a long while, if you see this then you can attach a function to the humanoid.Died event to destroy the proximity prompt, additionally you can destroy the prompt when you successfully revive someone. And finally when the character recovers from being knocked out, you can do a :GetFirstChild() on the torso to see if they have a proximity prompt still and delete it.

1 Like

I think it seems to be making a new proximity prompt everytime the health changes. How can I fix this?

		humanoid.HealthChanged:Connect(function()
			if humanoid.Health <= 50 then
				print("player incapacitated")
				
				local uhsclone = incapuhs:Clone()
				uhsclone.Parent = player.PlayerGui
				
				anim:Play()
				
				hrp.Anchored = true
				task.wait(5)
				hrp.Anchored = false
				
				wait(2.5)
				
				uhsclone:Destroy()
			
				
				
				local proxPrompt = Instance.new("ProximityPrompt")
				proxPrompt.Parent = player.Character:WaitForChild("Torso")
				print(proxPrompt.Parent)
				proxPrompt.RequiresLineOfSight = false
				proxPrompt.HoldDuration = 10
				proxPrompt.Triggered:Connect(function()
					humanoid.Health = 100
					humanoid.WalkSpeed = 1
					proxPrompt:FindFirstChild(proxPrompt):Destroy()
				end)
				
				while humanoid.Health <= 69 do
					wait(2)
					humanoid:UnequipTools()
				end	
				
			elseif humanoid.Health >= 70 then 
				print("player is recovering")				
				hrp.Anchored = false
				humanoid.WalkSpeed = 5		
				if humanoid.Health == 100 then
					print("player fully recovered or healthy")
					humanoid.WalkSpeed = 16
					
					anim:Stop()				
				end
			end;
		end);
	end;