How to get values from a table/array ([1] = value)

Hello! I got an issue with tables.

local materials = {
	[Enum.Material.WoodPlanks] = 9058073414,
	[Enum.Material.Wood] = 9058073414,
	[Enum.Material.Plastic] = 74510715852116,
	[Enum.Material.SmoothPlastic] = 74510715852116
}

I want to find the material the player is standing on (Humanoid.FloorMaterial) inside of the table and then take the sound id of it to play a sound.

I searched the internet however all I found is people asking about simple thing as these;

local t = {"cats"}

Which is not what im looking for

Thanks in advance!

NOTE: When giving me code please explain what things do because I have seen people just give code without any extra comments and call it a day.

I believe every enum has a .Value property, which is a unique number assigned to it. Something like this:

local materials = {
	[Enum.Material.WoodPlanks.Value] = 9058073414,
	[Enum.Material.Wood.Value] = 9058073414,
	[Enum.Material.Plastic.Value] = 74510715852116,
	[Enum.Material.SmoothPlastic.Value] = 74510715852116
}

All this may not be necessary though, because enums are hashable, you can use them as keys in a table.

4 Likes

materials[Humanoid.FloorMaterial]

1 Like

What does that do? Could you please provide more information?

if you assign a variable to that you get the sound id for the material

local soundID = materials[Humanoid.FloorMaterial]
1 Like

Hmm how do I do this…

Tried table.unpack and printed, returned nil.

Tried for loops, nothing…

What are you asking? If you’re asking how to determine which id to use, use the expression @Hzodx mentioned earlier:

local soundID = materials[Humanoid.FloorMaterial]

I think that it printed nil when I tried it

Wait I think that know whats wrong

Output is still printing out nil…

Do you maybe have another method for this?

Can you send the code? The full code.

I made many attempts on this one

local materials = {
	[Enum.Material.WoodPlanks] = 9058073414,
	[Enum.Material.Wood] = 9058073414,
	[Enum.Material.Plastic] = 74510715852116,
	[Enum.Material.SmoothPlastic] = 74510715852116
}
script.Parent.SpeedController.FootstepEvent.OnServerEvent:Connect(function()
	print("serverfootstep")
	--local un = table.unpack(materials, script.Parent.Humanoid.FloorMaterial.Value, script.Parent.Humanoid.FloorMaterial.Value)
	--print(un)
	--for material, id in pairs(materials) do
	--	if material == script.Parent.Humanoid.FloorMaterial.Value then
	--		print(material, id)
	--	end
	--end
	--table.find(materials, script.Parent.Humanoid.FloorMaterial.Value, 1)
	local soundid = materials[script.Parent.Humanoid.FloorMaterial]
	print(soundid)
	--fs.SoundId = table.find(materials, script.Parent.Humanoid.FloorMaterial, 1)
	--type(materials)
	--print(table.find(materials, script.Parent.Humanoid.FloorMaterial, 1))
end)

for loop worked only once, then mysteriously stopped working

This exact thing worked for me, it must be something else, are you getting any errors?

No errors in the output

(annoying character limit)

image
So thats why… hmm

I know the issue, I just had to put a tiny delay before firing the event on client side. Credits to @Hzodx for providing the solution to the first and main issue!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.