Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Pxnn7 0
2 years ago

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)
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 lamgogo 56 — 2y

1 answer

Log in to vote
0
Answered by
lamgogo 56
2 years ago

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)
Ad

Answer this question