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