Attempt index nil with GetChildren

for i ,v in pairs(Character:GetChildren()) do

attempt index nil with GetChildren

What? Are you new here, because we need to know the context of your situation bro. :confounded:

how to fix index nil with GetChildren

i use

local Character = Player.Character

for i ,v in pairs(Character:GetChildren()) do
if v:IsA(“MeshPart”) then
–CODE–

uh is that on local or server eeeeeeeeeeeeeeee

In this example character is equal to nil

Ok, you can use ` to refer to code in three characters before and after “```”.
Also I wouldn’t use the character for that since it’s a nil thing for :GetChildren, use the player instead, in a local script.

Where is this script located and is it a script or localscript?

the script is in server storage and is was local script

localscripts wont run in serverstorage

should i put it in sscriptservice?

What exactly are you trying to achieve?

put a particle inside every bodypart

When?
[character limit is pointless]

put a particle inside every bodypart when pressing J

1 Like

that just means that the character is nil. just do local character = player.CharacterAdded:Wait() or player.Character.

1 Like

In Local Scripts, use

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
-- Code for character / player

And for Server Scripts, use

game.Players.PlayerAdded:Connect(function(Player)
    local Character = Player.Character or Player.CharacterAdded:Wait()
    -- Code for character / player
end)

By using Player.CharacterAdded:Wait(), you ensure that the Character model has actually loaded before referencing. Additionally with your script, I don’t see a Player variable assigned.

1 Like

Player.CharacterAdded:Connect(function(character) end) would also work, but only if you want the event to fire when the character is added and when the player respawns.

Objects you will need: Localscript, Script, Remote event
image

LocalScript

local CAS = game:GetService("ContextActionService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local ParticleEvent = game.ReplicatedStorage:WaitForChild("ParticleEvent")

function GiveParticle(_,state)
	if not Character.Head:FindFirstChildOfClass("ParticleEmitter") and state == Enum.UserInputState.Begin then
		ParticleEvent:FireServer()
	end
end
CAS:BindAction("GiveParticles",GiveParticle,false,Enum.KeyCode.J)

Script

local ParticleEvent = game.ReplicatedStorage:WaitForChild("ParticleEvent")

ParticleEvent.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	if not Character then return end
	
	for i,v in pairs(Character:GetChildren()) do
		if v:IsA("BasePart") or v:IsA("MeshPart") then
			--Insert particles
		end
	end
end)