Won't grab from table despite given exact name?

Why does my script not work? It’s a local script placed inside the Player and “sound_type” is “gun_sounds”. I’m a bit confused and it’s probably really obvious but I just can’t tell right now.

local spawn_sounds = {"rbxassetid://13007732237", "rbxassetid://13007770756", "rbxassetid://13007770903"}
local gun_sounds = {"rbxassetid://13008184272", "rbxassetid://13008184104", "rbxassetid://13008183801", "rbxassetid://13008183584"}
local sound_Types = {gun_sounds, spawn_sounds}

vocal_event.OnClientEvent:Connect(function(sound_type)
	local index
	for i, v in pairs(sound_Types) do
		if v == sound_type then
			index = i
			print("got index")
			break
		else
			print("not correct index!!: "..i)
		end
	end
	if index then
		local soundid = sound_Types[index][math.random(1, #sound_Types[index])]

		local vocalsound = Instance.new("Sound", workspace)
		vocalsound.SoundId = soundid
		vocalsound.PlayOnRemove = true
		vocalsound:Destroy()
		print("sound played")
	end
end)

not correct index!!: 1
not correct index!!: 2

If it’s something obvious or you need more info just tell me.

1 Like

Does this work?

local sound_Types = {GunSounds = gun_sounds, SpawnSounds = spawn_sounds}

if i == sound_type then
			index = i
			print("got index")
			break
		else
			print("not correct index!!: "..i)
		end

And put sound_type as either GunSounds or SpawnSounds

What is sent by the server in sound_type? Can you print it out?

What do you mean by that? Like what is the object?

Doesn’t work sadly. And I’m not manually changing “sound_type”.

Why can’t you just change whatever is sending the value?
Also I think what @Pokemoncraft5290 meant was what the value of sound_type was?

1 Like

sound_type = "gun_sounds"

sound_type will be a universal term in whenever I am requesting a sound. A event is going to be sending the sound_type. Then a local script get’s the table that sound_type refers to and from there picks a random sound from the table to play.

I don’t know if I misunderstood you two but I can technically change sound_type but it will change depending on whatever is firing the script.

hold on I might be being an idiot rn
Nvm this still doesn’t work even changing the sound names and everything

This should work without any changes from other scripts:

local spawnsounds = {"rbxassetid://13007732237", "rbxassetid://13007770756", "rbxassetid://13007770903"}
local gunsounds = {"rbxassetid://13008184272", "rbxassetid://13008184104", "rbxassetid://13008183801", "rbxassetid://13008183584"}

local sound_Types = {gun_sounds = gunsounds, spawn_sounds = spawnsounds}

if i == sound_type then
	index = i
	print("got index")
	break
else
	print("not correct index!!: "..i)
end
1 Like

is there a reason you don’t make the table that contains the other tables a dictionary instead? I feel like that would be a lot easier. Something like this?

local spawn_sounds = {"rbxassetid://13007732237", "rbxassetid://13007770756", "rbxassetid://13007770903"}
local gun_sounds = {"rbxassetid://13008184272", "rbxassetid://13008184104", "rbxassetid://13008183801", "rbxassetid://13008183584"}
local sound_Types = {gunSounds=gun_sounds, spawnSounds=spawn_sounds}

vocal_event.OnClientEvent:Connect(function(sound_type)
local useTable= sound_Types[sound_type]
local soundid = useTable[math.random(1, #useTable)]

just now realized this post was solved

1 Like

I don’t know I didn’t really think of it like that I guess

1 Like

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