How would this be solved if values are nil?

Okay so today I was starting my work on a script that is supposed to be user-friendly, where they can just insert sound IDs to replace the default sounds made from walking, jumping, etc. In a for loop, I was trying to find the number of items in an array, but eventually got the problem that the # function does not count nil items, making this only count for the first time. Can someone please explain how I would change this from the current “9” that is there, to be more accurate if deleting or adding more sounds?

THE ENTIRE SCRIPT:

local Players = game:GetService("Players")

--[[
The SoundIds that can be assigned. When changing, use example:
local example = "rbxassetid://5721510093"
When adding your own sounds, add "id" to the end of "rbxasset"

--]]

Players.PlayerAdded:Connect(function(player)
	
	--These are the variables you change
	local Running = 5721510093
	local Died = nil
	local Climbing = nil
	local FreeFalling = nil
	local GettingUp = nil
	local Jumping = nil
	local Landing = nil
	local Splash = nil
	local Swimming = nil
	
	local RunningSoundId = "rbxasset://sounds/action_footsteps_plastic.mp3"
	local DiedSoundId = "rbxasset://sounds/uuhhh.mp3"
	local ClimbingSoundId = "rbxasset://sounds/action_footsteps_plastic.mp3"
	local FreeFallingSoundId = "rbxasset://sounds/action_falling.mp3"
	local GettingUpSoundId = "rbxasset://sounds/action_get_up.mp3"
	local JumpingSoundId = "rbxasset://sounds/action_jump.mp3"
	local LandingSoundId = "rbxasset://sounds/action_jump_land.mp3"
	local SplashSoundId = "rbxasset://sounds/impact_water.mp3"
	local SwimmingSoundId = "rbxasset://sounds/action_swim.mp3"
	
	
	
	local setVariables = {
		Running,
		Died,
		Climbing,
		FreeFalling,
		GettingUp,
		Jumping,
		Landing,
		Splash,
		Swimming
	}
	
	local defaultSounds = {
		RunningSoundId,
		DiedSoundId,
		ClimbingSoundId,
		FreeFallingSoundId,
		GettingUpSoundId,
		JumpingSoundId,
		LandingSoundId,
		SplashSoundId,
		SwimmingSoundId
	}
	print(#setVariables)
	for x=1, 9, 1 do 
		if setVariables[x] ~= nil then
			setVariables[x]=defaultSounds[x]
			print("Sound Number " .. x .. " Was Changed by Script to Sound Id " .. setVariables[x])
		else
			print("Sound Number " .. x .. " Will Stay as Default Sound")
			print(setVariables[x])
		end
	end
	
	
	
	
	
	
	
	
	
	wait(5)
	local sounds = player.HumanoidRootPart
	sounds.Running.SoundId=defaultSounds
	sounds.Died.SoundId=DiedSoundId
	sounds.Climbing.SoundId=ClimbingSoundId
	sounds.FreeFalling.SoundId=FreeFallingSoundId
	sounds.GettingUp.SoundId=GettingUpSoundId
	sounds.Jumping.SoundId=JumpingSoundId
	sounds.Landing.SoundId=LandingSoundId
	sounds.Splash.SoundId=SplashSoundId
	sounds.Swimming.SoundId=SwimmingSoundId
end)

THE PROBLEM AREA:

for x=1, 9, 1 do

Thanks for all the help in advance :grinning:

you can do table.getn(table) to get the number of elements inside it.

Maybe X is an instance…? Try doing tonumber(x) at the if statement

@WooleyWool
x is the number of the item of the array that it is currently on, shown on the line above. Every time the loop repeats, x goes up 1

1 Like

Are either of the print statements being reached?

All of them are working. Just to point out, the print(#setVariables) was changed to what @minimic2002 said, to print(table.getn(newSounds) also defaultSounds was changed to be newSounds

I haven’t replied to this topic in a while so I figured id share a new perspective as I have started properly implementing modules. I would recommend using a module to do the majority of the work.

At the moment if you want to add more sounds then you would have to add to the script, I think making it more universal will save you time and also keep things organised.

Keep the default sounds in the same location so you can fetch then.
As for adding new animations you could put them all in a folder that can be found.
This way rather than creating a list for each manually you can do something along the lines of:

local Sounds = {}
local Hum = player.Humanoid

-- Get Default Sounds
local C = Hum:GetChildren()
for i = 1,#C do
    if C[i].ClassName == "Sound" then
        table.insert(Sounds,C[i])
    end
end

-- Get Sounds From Folder
C = Hum.SoundsFolder:GetChildren()
for i = 1,#C do
    if C[i].ClassName == "Sound" then
       table.insert(Sounds,C[i])
    end
end

as defaultSounds do not contain nils, you could do this

for x=1, #defaultSounds do 

for statements would go through everything including nils anyway.

Getting all the sounds as opposed to their properties is a better way to go.
this means that if you wanted to change the sound you could just change it using the sound in the table rather than trying to find it again.

Another possible solution is instead of using nils, use something that is obviously not an asset. for example

--These are the variables you change
	local Running = 5721510093
	local Died = 0
	local Climbing = 0
	local FreeFalling = 0
	local GettingUp = 0
	local Jumping = 0
	local Landing = 0
	local Splash = 0
	local Swimming = 0

now you can use #setVariables and do if setVariables[x] ~= 0 then

In such cases, most programs use -1 instead of 0