This script is supposed to make custom death sounds for a certain team, And when the player is on that certain team the sound doesnt play for the player and there are no errors. Please help me fix this annoying bug.
combine = false
local players = game:GetService("Players")
local player = players.LocalPlayer
local function randomSound()
local SongList = workspace.Combinedeathsounds:GetChildren() -- Change this to your own path
local randomNumber = math.random(1, #SongList)
for i, v in pairs(SongList) do
if i == randomNumber then
local sound = v:Clone()
sound.Parent = script.Parent
sound:Play()
end
end
end
game.Players.PlayerAdded:Connect(function(plr)
if plr.Team == "Combines" then
combine = true
else
combine = false
end
end)
script.Parent.Humanoid.Died:Connect(function(player)
if combine == true then
randomSound()
else
warn("Player is not a combine")
end
end)
I can’t really tell for sure because of the weird indentation of the code, but it looks like the PlayerAdded and .Died functions are inside of the randomSound() function, and wont work unless randomSound() is called. Are you get any prints on the .Died function?
Also, a small nitpick.
You can get a random sound a much easier way
local num = math.random(#SongList)
local sound = SongList[num]:Clone()
Oh, I can see where it closes now. You’re indentation is really confusing. I can see the real problem now.
You can’t compare a team with a string, so it’s returning nil. You should compare the player’s TeamColor to the Combines TeamColor. For extra simplicity, since you’re using a boolean, you can set combine equal to that check.
I’m not entirely sure when teams are loaded, so you may need to add a wait to make sure the player’s team is properly set. Also, if the team can change you will need to update combine accordingly.