Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What does this "attempt to concatenate field '?' (a nil value)" error message mean?

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

0

Where do you define 'WoodSounds' or 'SandSounds', etc.?

0

In a few tables for each material set.

0

just as "1234567" but real ids

0

The parent argument to Instance.new is deprecated. Do not use it.

0

It says this 442, which is this line of code is always the problem: Meta.SoundId = "rbxassetid://" .. ConcreteSounds[math.random(0,#ConcreteSounds)]

0

^ 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.

0

I thought they were from 0 oof thanks

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.