Parts[i] is nil

So I have a code that goes like this…

local parent = script.Parent
local parts = {
	parent.Part1,
	parent.Part2,
	parent.Part3
--And my other parts
}

local function toggleCollision(bool)
	for i=0,#parts,1 do
		parts[i].CanCollide = bool
	end
end

toggleCollision(true)
--It continues with more stuff and toggleCollisions.

but in the Output I get an error saying I’m indexing CanCollide to nil (parts[i]) and it gets me kinda confused because I used to use a print and it would print every part.
EVEN WORSE, out of nowhere it stopped printing and I found out the bug was stopping the script from working (and it wasn’t doing that before). So I put a print before the error, and it printed nil!! Out of nowhere!1! :tired_face:

It’s a simple issue, with a simple fix! So in any other programming language you’d expect an array to start at 0 - but in Luau a table starts at index 1. Parts[0] doesn’t exist, so trying to set CanCollide of a nil reference will not work. All you need to do is change for i=0 to for i=1.

There is also another way to cycle through a list too that will avoid this problem. That’s using a ‘for-pair loop’, which automatically cycles through every item in a list without having to specify where i starts and ends. It looks like this:

for i, part in parts do
	part.CanCollide = bool
end

Oh thanks! Also, bonus question, is it possible to change a player’s gravity to make him stay in air more time?

1 Like

Yes! You can insert a VectorForce (not VectorVelocity, a Force = add speed, Velocity = replace speed) into the character’s HumanoidRootPart. From there you’ll need to check how massive the player is (character.PrimaryPart.AssemblyMass). iirc the formula for making something zero-gravity is YForce = game.Workspace.Gravity * AssemblyMass and then plugging the variable into VectorForce.Force = Vector3.new(0,YForce,0). To make something floaty, but still affected by gravity, you’d add a multiplier between 0 and 1.

local factor = 0.5 -- change this number to affect floatyness
local character: Model = ... -- character here

local hrp = character.PrimaryPart
local mass = hrp.AssemblyMass
local vectorForce = Instance.new("VectorForce")
vectorForce.Name = "Floatyness"
vectorForce.RelativeTo = Enum.ActuatorRelativeTo.World
vectorForce.Attachment0 = hrp.RootAttachment
vectorForce.Parent = hrp
vectorForce.Force = Vector3.new(0, game.Workspace.Gravity * mass * factor, 0)

That is… so complex for my newbie brain :sweat_smile:
I guess I’ll put that in StarterCharacterScripts? Or StarterPlayerScripts?
And why is the character variable being defined like character: Model =

Edit: How do I set the character? Or is it already being set by some dark magic? Should I use game:GetService("Players").LocalPlayer.Character (does that even exist? I forgor)

1 Like

If you want the player to always have low gravity, you’d put this inside a script inside StarterCharacterScripts. Then you’d declare local character = script.Parent.

Me putting : Model in front of the variable is something called Typechecking. It doesn’t actually do anything to your script and isn’t important, but it’s a neat little trick that can make scripting easier for you! In this example, Roblox Studio now knows that the ‘character’ variable is always referencing a Model. Therefore, when you script with that variable, intellisense (the scripting auto-suggestion thing) suggests you all properties and methods related to that thing :slight_smile:
image

Interesting :open_mouth:
And I’m getting this error sadly

RootAttachment is not a valid member of Part "Workspace.RememberToSmile01.HumanoidRootPart"

Oh oops, change the line vectorForce.Attachment0 = hrp.RootAttachment to vectorForce.Attachment0 = hrp:WaitForChild("RootAttachment"). If that doesn’t work, create your own attachment and use that instead.

It says it could be an infinite wield now. How do i make my own? I already have a StarterCharacter set. (That’s probably the issue)

Aah yes, that’s why this is happening. RootAttachment is a default Attachment that is in the standard Roblox character. To create your own, simply either insert an Attachment to the HumanoidRootPart of your custom StarterCharacter, or add this to your script:

local rootAttachment = Instance.new("Attachment")
rootAttachment.Parent = character.PrimaryPart

Then change vectorForce.Attachment0 to this.

Thanks, it worked! Unfortunately, I just realized now the game is better without floating :downcast_face_with_sweat:

1 Like

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