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