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