"Attempt to index nil with LeftArm"-Issues with turning off CanCollide

I’m trying to turn off a players CanCollide upon joining, but my LocalScript (line three) gives me the error message “attempt to index nil with LeftArm”

I’ve run through all possible scenarios that i am aware of ( keep in mind, theres probably a better way to do this that i dont know of ), and nothing has worked so far. Yes, ive searched the DevForum.

local name = game.Players.LocalPlayer.Name 
local plrmodel = game.Workspace:FindFirstChild(name) --//trying to find the players model in workspace//--
local bodyparts = plrmodel.LeftArm and plrmodel.RightArm and plrmodel.Torso and plrmodel.LeftLeg and plrmodel.RightLeg --//trying to get the player's limbs//--

local function canCollideFalse()
	if bodyparts.CanCollide == true then
		bodyparts.CanCollide = false --//turning off cancollide//--
	else
		print("CanCollide was already true.")
	end
end

game.Players.PlayerAdded:Connect(function()
	canCollideFalse() --//calling the function//--
end)

I’ve locked the game to R6, and this is a LocalScript inside of StarterCharacterScripts.
Can anyone explain what i’m doing wrong?

Use:

local player = game.Players.LocalPlayer
local name = player.Name
local plrCharacter = player.Character or player.CharacterAdded:Wait()

Or, since it’s inside the StarterCharacterScripts
use:

local character = script.Parent
1 Like

All limb names in an R6 character have a space in between the limb position and limb name, so for example it is Left Arm not LeftArm.

1 Like

(since the game isnt public i just copy and pasted my player rig and tested the cancollide)
the arms seem to have cancollide off, but the torso still doesn’t

Problem with this is that adding the space makes the script think that Arm is text.

image

Your code has a lot of problems, so I’ll help you fix them all.

First of all, this is a server script, which means that Players.LocalPlayer is not defined.

Second of all;

This is a boolean statement. Not an array. You are using the and operator/gate wrong which is used to check if both a and b is true. example:

(true and true) -- true
(true and false) -- false
(false and false) -- false

If needed, search up “and gate” for more details.

The correct way to do this is by making an iterable object and storing the limbs inside it, which would look something like this:

local character = ...
local limbs = {}

for _, limb in character:GetChildren() do
  table.insert(limbs, limb)
end

Or an even better method is to directly perform the operation when it is iterated over. Example:

local character = ...

local function do_operation(limb)
  limb.CanCollide = false
end

for _, child in character:GetChildren() do
  if child:IsA("BasePart") then
    do_operation(child)
  end
end

Third of all, you’re performing the operation only on the LocalPlayer (which is nil) You should use the provided player argument when using Players.PlayerAdded. Example:

local function do_something(plr)
  ...
end

Players.PlayerAdded(function(player)
  do_something(plr)
end)

No offense but, you seem to not know the basics of scripting. I would recommend refreshing your knowledge.

1 Like

image
???

yes i know that it says that one is disabled, i was doing some experimenting before you created this post

oh… i assumed that and was used like the literal word and didnt do any research on it… thanks for pointing that out

yeah im not that great at scripting yet. i only know a few things, like printing, functions, variables, loops, etc.
i think ill take a while to do more studying before trying to make my own script. thanks for the help!

That is because the compiler treats spaces as EOL’s. You can get around that by something["name"]

1 Like

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