So I'm trying to get my gun system to create an impact sound and play a random sound from a table of sound IDs. It works.. ish. Sometimes the sound will play, but when they don't it throws this error. I'm detecting impacts with the client and firing it over using a RemoteEvent to the server to execute the hit code. Got no idea what's going on here.
--// Client Code
hitEvent:FireServer(p,cross,angle,normal,"Part",h)
--// Server Code
local Meta = Instance.new("Sound")
Meta.Name = "Crack"
if h.Material == Enum.Material.Grass or h.Material == Enum.Material.Ice or h.Material == Enum.Material.Fabric or h.Material == Enum.Material.Pebble then
Meta = Crack:FindFirstChild('Crack') or Instance.new('Sound',Crack)
Meta.Name = 'Crack'
Meta.SoundId = "rbxassetid://" .. DirtSounds[math.random(0,#DirtSounds)]
end
if h.Material == Enum.Material.Sand then
Meta = Crack:FindFirstChild('Crack') or Instance.new('Sound',Crack)
Meta.Name = 'Crack'
Meta.SoundId = "rbxassetid://" .. SandSounds[math.random(0,#SandSounds)]
end
if h.Material == Enum.Material.Glass or h.Material == Enum.Material.Neon then
Meta = Crack:FindFirstChild('Crack') or Instance.new('Sound',Crack)
Meta.Name = 'Crack'
Meta.SoundId = "rbxassetid://" .. GlassSounds[math.random(0,#GlassSounds)]
end
if h.Material == Enum.Material.Wood or h.Material == Enum.Material.WoodPlanks then
Meta = Crack:FindFirstChild('Crack') or Instance.new('Sound',Crack)
Meta.Name = 'Crack'
Meta.SoundId = "rbxassetid://" .. WoodSounds[math.random(0,#WoodSounds)]
end
if h.Material == Enum.Material.Metal or h.Material == Enum.Material.CorrodedMetal or h.Material == Enum.Material.DiamondPlate then
Meta = Crack:FindFirstChild('Crack') or Instance.new('Sound',Crack)
Meta.Name = 'Crack'
Meta.SoundId = "rbxassetid://" .. MetalSounds[math.random(0,#MetalSounds)]
end
if h.Material == Enum.Material.Water then
Meta = Crack:FindFirstChild('Crack') or Instance.new('Sound',Crack)
Meta.Name = 'Crack'
Meta.SoundId = "rbxassetid://" .. WaterSounds[math.random(0,#WaterSounds)]
end
if h.Material == Enum.Material.Concrete or h.Material == Enum.Material.Plastic or h.Material == Enum.Material.SmoothPlastic or h.Material == Enum.Material.Slate or h.Material == Enum.Material.Foil or h.Material == Enum.Material.Brick or h.Material == Enum.Material.Granite or h.Material == Enum.Material.Marble or h.Material == Enum.Material.Cobblestone then
Meta = Crack:FindFirstChild('Crack') or Instance.new('Sound',Crack)
Meta.Name = 'Crack'
Meta.SoundId = "rbxassetid://" .. ConcreteSounds[math.random(0,#ConcreteSounds)]
end
Meta.Volume = math.random(0.8,1)
Meta.EmitterSize = 10
Meta.MaxDistance = 30
Meta.Pitch = math.random(0,0.6)
Meta.Parent = Crack
Meta:play()
--// Error Message
17:10:12.344 - Stack Begin
17:10:12.344 - Script 'Players.StoneFox_Alfa.Backpack.Editing Model.Program_Server', Line 442
17:10:12.345 - Stack End
17:10:12.443 - Players.StoneFox_Alfa.Backpack.Editing Model.Program_Server:442: attempt to concatenate field '?' (a nil value)

7 comments
Where do you define 'WoodSounds' or 'SandSounds', etc.?
In a few tables for each material set.
just as "1234567" but real ids
The parent argument to Instance.new is deprecated. Do not use it.
It says this 442, which is this line of code is always the problem: Meta.SoundId = "rbxassetid://" .. ConcreteSounds[math.random(0,#ConcreteSounds)]
^ Lua table indexes are from 1 and up, you would want it to be ConcreteSounds[math.random(1,#ConcreteSounds)]. The error just means that you're concatenating (..'ing) something that's nil, and ConcreteSounds[0] is going to be nil.
I thought they were from 0 oof thanks