-
What do you want to achieve? So i made a script that it’s supposed to play random damage sounds when player gets damaged and waits until the damage sound ends to play the next one when getting damaged
-
What is the issue? It keeps playing when i’m not getting damaged and doesn’t work after player resets
-
What solutions have you tried so far? i didn’t found any solution
this is a local script in starterplayerscripts
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local sounds = {
"rbxassetid://not going to show",
"rbxassetid://not going to show",
"rbxassetid://not going to show",
"rbxassetid://not going to show",
"rbxassetid://not going to show",
"rbxassetid://not going to show",
"rbxassetid://not going to show"
}
local soundFolder = Instance.new("Folder")
soundFolder.Name = "DamageSounds"
soundFolder.Parent = LocalPlayer:WaitForChild("PlayerGui")
local soundObjects = {}
for i, soundId in ipairs(sounds) do
local sound = Instance.new("Sound")
sound.SoundId = soundId
sound.Volume = 1
sound.Name = "Sound" .. i
sound.Parent = soundFolder
table.insert(soundObjects, sound)
end
local canPlaySound = true
local function playRandomSound()
if canPlaySound then
canPlaySound = false
local randomIndex = math.random(1, #soundObjects)
local selectedSound = soundObjects[randomIndex]
selectedSound:Play()
selectedSound.Ended:Wait()
canPlaySound = true
end
end
humanoid.HealthChanged:Connect(function(health)
if health < humanoid.MaxHealth then
playRandomSound()
end
end)