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

Can anyone help me with my script?

Ok, so I want to show a red GUI that is transparent (.7) then show a message that says "Danger: Low Health" then I want the sound of a heart beating to go ONLY if the player as less than 25 health. But the sound will not turn off if the player has more Cany anyone help me?

textlabel = script.Parent warningdis = script.Parent.Parent player = game.Players.LocalPlayer humanoid = player.Character.Humanoid while wait() do textlabel:WaitForChild ("heartbeat") if humanoid.Health <=25 then heartbeat = Instance.new("Sound",textlabel) heartbeat.Name = heartbeat heartbeat.SoundId = "rbxassetId://131038747" heartbeat.Looped = true end if heartbeat.Health >25 then heartbeat:Destroy() end while true do if humanoid.Health <=25 then warningdis.Visible = true textlabel.Text = "Warning: Low Health!" end if humanoid.Health >25 then warningdis.Visible = false end wait() end end

1 comment

0

Please highlight all of your code and click on the blue Lua button so we can read what your script is.

1 answer

✓ Accepted answer

Sorted code:

textlabel = script.Parent
warningdis = script.Parent.Parent 
player = game.Players.LocalPlayer 
humanoid = player.Character.Humanoid 

while wait() do
textlabel:WaitForChild ("heartbeat") 
	if humanoid.Health <=25 then 
		heartbeat = Instance.new("Sound",textlabel) 
		heartbeat.Name = heartbeat 
		heartbeat.SoundId = "rbxassetId://131038747" 
		heartbeat.Looped = true
		heartbeat:Play() --Play it
end 

if heartbeat.Health > 25 then 
	heartbeat:Stop() --Stop it
	heartbeat:Destroy() 
end 

while true do --Loop inside another loop, + no wait will crash game/studio.
	if humanoid.Health <=25 then 
		warningdis.Visible = true 
		textlabel.Text = "Warning: Low Health!" 
end 

	if humanoid.Health > 25 then 
			warningdis.Visible = false 
		end 
		wait() 
	end 
end

Solution:

local textlabel = script.Parent
local warningdis = script.Parent.Parent 
local player = game.Players.LocalPlayer 
local humanoid = player.Character.Humanoid 

function CreateSound()
	textlabel:WaitForChild ("heartbeat")
		if humanoid.Health <= 25 then 
			heartbeat = Instance.new("Sound" player.PlayerGui) 
			heartbeat.Name = heartbeat 
			heartbeat.SoundId = "rbxassetId://131038747" 
			heartbeat.Looped = true
			heartbeat:Play()
	end
end 

while wait() do
	if humanoid.Health <= 25 then 
		warningdis.Visible = true 
		textlabel.Text = "Warning: Low Health!" 
		CreateSound() --Make + Play the sound.
	elseif humanoid.Health >= 25 then
		if player.PlayerGui:FindFirstChild("heartbeat") then
			player.PlayerGui:FindFirstChild("heartbeat"):Stop()
			player.PlayerGui:FindFirstChild("heartbeat"):Destroy()
			warningdis.Visible = false
			textlabel.Text = " " --Whatever text you want.
		end
	end
end

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