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

How could i make my script that picks random sounds in the humanoids head, work?

Hi, My Name Is Blue, and i have this script that can pick a random sounds in the head, and for some reason its dosent work and i dont really know why,

this is the script

repeat wait() until script.Parent:WaitForChild("HurtSounds"):GetDescendants()

local Humanoid = script.Parent:WaitForChild("Humanoid")
local LastHealth = Humanoid.Health

Humanoid.HealthChanged:Connect(function()
	if Humanoid.Health > LastHealth then
		LastHealth = Humanoid.Health
	end
	if Humanoid.Health < LastHealth and LastHealth > 30 then
		LastHealth = Humanoid.Health
		local Tracks = script.Parent:WaitForChild("Head"):GetDescendants()
		local RandomTracks = math.random(1,#Tracks)
		local PickedTrack = Tracks[RandomTracks] 
		if PickedTrack ~= nil then
			PickedTrack:Play()
			print("im hurt")
		end
	end
end)

1 comment

0

at the line 4,the local thingy will always update,so in ur case lasthealth will just show the player's health and always update with player's health

1 answer

ok so spawn(function() maybe can fix ur problem,using it will run on another thread and dont effect ur script,and it even have the ability to save variables(after sending them to function)

repeat wait() until script.Parent:WaitForChild("HurtSounds"):GetDescendants()

local Humanoid = script.Parent:WaitForChild("Humanoid")
local function healthChanged(lastHealth)
 if Humanoid.Health > LastHealth then
        LastHealth = Humanoid.Health
    end
    if Humanoid.Health < LastHealth and LastHealth > 30 then
        LastHealth = Humanoid.Health
        local Tracks = script.Parent:WaitForChild("Head"):GetDescendants()
        local RandomTracks = math.random(1,#Tracks)
        local PickedTrack = Tracks[RandomTracks]
        if PickedTrack ~= nil then
            PickedTrack:Play()
            print("im hurt")
        end
    end
end
Humanoid.HealthChanged:Connect(function()
spawn(function()
healthChanged(LastHealth)
end)
end)

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