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

I'm trying to make my npc make a random punch sound when he hits someone, but it wont work?

So I keep getting this error "attempt to call a table value" and I know its probably a stupid mistake but I really don't understand what to do, this is my code

	local punchSounds = {"rbxassetid://1193237596", "rbxassetid://1033173157", "rbxassetid://131237241", "rbxassetid://3064939784"} -- this is the variable set before the other line	


		local punchSound = Instance.new("Sound")
		punchSound.SoundId = punchSounds(math.random(1, 4)) -- the line thats messing up
		punchSound.Volume = 1
		punchSound.Parent = script.Parent:FindFirstChild("Right Arm")
		punchSound.Name = "punch"
		punchSound:Play()

2 answers

✓ Accepted answer

you acces a table by [] not ()
and here i made a slight modification, so it just picks a random sound from your array no matter how big it is

local soundId = punchSounds[math.random(#punchSounds)]

# gives you the amount of things inside an array

1 comment

0

it worked! thanks dude!

well, another way you could do this is you could have 4 sounds stored somewhere, name them "sound1" - "sound4" then,

script.Parent["sound"..math.random(1,4)]:Play()

that's how i would do it, unless there's some underlying reason why you cant do it that way

3 comments

0

i would accept the other answer this is just antoher way to do it

0

i like to use arrays with soundids because its way more clean to me than making 4 or more sounds and having it go through them when it could make 1 sound and have it over with

0

alr, i was just throwing stuff out there lol

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