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)
To "concatenate" means to "link together" which means that you are trying to make it as one. Sorta like adding 1+2 but in this case, you cannot connect something that doesn't exist with a value.
What I recommend doing is adding an "if" statement that checks whether "h" is not "nil."
if h ~= nil then --Your code here end
If you have any questions or issues, please contact me. ;)