Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

attempt to index nil with 'Character'?

My Character script doesn't work.

script.Parent.MouseButton1Click:Connect(function()
local plrsearch = script.Parent.Text
local plr = game.Players:FindFirstChild(plrsearch)
local chr = plr.Character
local leftarm = chr["Left Arm"]
local rightarm = chr["Right Arm"]
local leftleg = chr["Left Leg"]
local rightleg = chr["Right Leg"]
local function removelimbs()
    pcall(function()
        game:GetService("ReplicatedStorage").Limb:FireServer((leftarm))
        game:GetService("ReplicatedStorage").Limb:FireServer((rightarm))
        game:GetService("ReplicatedStorage").Limb:FireServer((leftleg))
        game:GetService("ReplicatedStorage").Limb:FireServer((rightleg))
    end)
end
removelimbs()
end)

The error starts at line 4 with code error attempt to index nil with 'Character'. My Script Basically searches for Players.

2 comments

0

what is script.Parent, is it a TextBox or a TextButton

0

Ziannea it's a TextBox.

2 answers

The character may not have been loaded yet, use plr.Character or plr.CharacterAdded:Wait(). Also your variable for plr is incorrect and fix your indentation. Also I suggest you reference the variables first. Also, I don't seem to understand why did you wrap your function inside a pcall function.

-- Variables

local plrsearch = script.Parent.Text

local plr = game.Players.LocalPlayer

local chr = plr.Character or plr.CharacterAdded:Wait()

local leftarm = chr["Left Arm"]

local rightarm = chr["Right Arm"]

local leftleg = chr["Left Leg"]

local rightleg = chr["Right Leg"]

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Limb = ReplicatedStorage:WaitForChild("Limb")

script.Parent.MouseButton1Click:Connect(function()

local function removelimbs()
    pcall(function()
        Limb:FireServer(leftarm)
        Limb:FireServer(rightarm)
        Limb:FireServer(leftleg)
        Limb:FireServer(rightleg)
    end)
end
removelimbs()
end)

Well your player variable is wrong. Also, make sure this is a localscript:

	script.Parent.MouseButton1Click:Connect(function()
	local plrsearch = script.Parent.Text
	local plr = game.Players.LocalPlayer
	local chr = plr.Character
	local leftarm = chr["Left Arm"]
	local rightarm = chr["Right Arm"]
	local leftleg = chr["Left Leg"]
	local rightleg = chr["Right Leg"]
	local function removelimbs()
	    pcall(function()
	        game:GetService("ReplicatedStorage").Limb:FireServer((leftarm))
	        game:GetService("ReplicatedStorage").Limb:FireServer((rightarm))
	        game:GetService("ReplicatedStorage").Limb:FireServer((leftleg))
	        game:GetService("ReplicatedStorage").Limb:FireServer((rightleg))
	    end)
	end
	removelimbs()
	end)

2 comments

0

But I want it to search for the player from the GUI.

0

Even it is, character may not have loaded yet..

This discussion is preserved as it was. New answers, comments, and votes are closed.