LocalPlayer error

Hello there, I have been trying to fix something. I know that it’s not the correct code for this line but I cannot find the code it should be. Any help?

local plr = game.Players.LocalPlayer (This should not be this, this is the line)
local mouse = plr:GetMouse()

function onClick()
if plr.Character:FindFirstChild(“RightArm1”) == nil then
local g = script.Parent.Parent.Backpack:Clone()
g.Parent = plr.Character
local C = g:GetChildren()
for i=1, #C do
if C[i].className == “Part” or C[i].className == “UnionOperation” or C[i].className == “WedgePart” or C[i].className == “MeshPart” then
local W = Instance.new(“Weld”)
W.Part0 = g.Middle
W.Part1 = C[i]
local CJ = CFrame.new(g.Middle.Position)
local C0 = g.Middle.CFrame:inverse()*CJ
local C1 = C[i].CFrame:inverse()*CJ
W.C0 = C0
W.C1 = C1
W.Parent = g.Middle
end
local Y = Instance.new(“Weld”)
Y.Part0 = plr.Character.Torso
Y.Part1 = g.Middle
Y.C0 = CFrame.new(0, 0, 0)
Y.Parent = Y.Part0
end

local h = g:GetChildren()
for i = 1, # h do
    if h[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart" or C[i].className == "MeshPart" then
        h[i].Anchored = false
        h[i].CanCollide = false
    end
end

end
end

mouse.Button1Down:connect(onClick)

This is a serverscript.

image

You cannot use LocalPlayer in server scripts, please use a Local Script, and the first argument in the connection from the click is the player. This should be the script:

function onClick(plr)
if plr.Character:FindFirstChild(“RightArm1”) == nil then
local g = script.Parent.Parent.Backpack:Clone()
g.Parent = plr.Character
local C = g:GetChildren()
for i=1, #C do
if C[i]:IsA( “BasePart”) then
local W = Instance.new(“Weld”)
W.Part0 = g.Middle
W.Part1 = C[i]
local CJ = CFrame.new(g.Middle.Position)
local C0 = g.Middle.CFrame:inverse()*CJ
local C1 = C[i].CFrame:inverse()*CJ
W.C0 = C0
W.C1 = C1
W.Parent = g.Middle
end
local Y = Instance.new(“Weld”)
Y.Part0 = plr.Character.Torso
Y.Part1 = g.Middle
Y.C0 = CFrame.new(0, 0, 0)
Y.Parent = Y.Part0
end

local h = g:GetChildren()
for i = 1, # h do
    if h[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart" or C[i].className == "MeshPart" then
        h[i].Anchored = false
        h[i].CanCollide = false
    end
end

You can’t access LocalPlayer in server scripts. Make it a LocalScript, and move it into a place where it will run.

A local script wouldn’t show that the person has morphed to other players though?

No, but you can try to do something (I don’t have ideas)

Its fine to use a Server Sided script with a clickdetector,
youre using the Clickdetector wrong More Info about ClickDetector here

Also it is not
mouse.Button1Down:connect(onClick)

Its this:
script.Parent.ClickDetector.MouseClick:Connect(OnClick) and with the code that @TheTurtleMaster_2 has provided

The issue I have here is LocalPlayer but thanks for addressing other issues

Note: And connect works, it is deprecated but still works fine

What do you mean with Localplayer? the ClickDetector grabs the player that clicked it

1 Like

Sorry, I forgot to specify. These are the lines of code, if you read through further it has further plr.Character things.

If im reading this correct you want too give the player that clicked on the ClickDetector the BackPack Model right?

Yes, let me explain.

When the person clicks on the part the BackPack model will attach onto the player like a normal morph would

Then yes the ClickDetector grabs the player that clicked on the ClickDetector and gives that player the Backpack

No need to use LocalPlayer, ClickDetector automatically gives the player that clicked it

Sorry, I’m confused as how this would be implemented into the code…?

function onClick(plr)
if plr.Character:FindFirstChild(“RightArm1”) == nil then
local g = script.Parent.Parent.Backpack:Clone()
g.Parent = plr.Character
local C = g:GetChildren()
for i=1, #C do
if C[i]:IsA( “BasePart”) then
local W = Instance.new(“Weld”)
W.Part0 = g.Middle
W.Part1 = C[i]
local CJ = CFrame.new(g.Middle.Position)
local C0 = g.Middle.CFrame:inverse()*CJ
local C1 = C[i].CFrame:inverse()*CJ
W.C0 = C0
W.C1 = C1
W.Parent = g.Middle
end
local Y = Instance.new(“Weld”)
Y.Part0 = plr.Character.Torso
Y.Part1 = g.Middle
Y.C0 = CFrame.new(0, 0, 0)
Y.Parent = Y.Part0
end

local h = g:GetChildren()
for i = 1, # h do
if h[i].className == “Part” or C[i].className == “UnionOperation” or C[i].className == “WedgePart” or C[i].className == “MeshPart” then
h[i].Anchored = false
h[i].CanCollide = false
end
end
script.Parent.ClickDetector.MouseClick:Connect(onClick)

Sorry code blocks wont work

These parts don’t seem to work,

image

image

image

Give me a couple minutes to review the code and indent and cleanup

1 Like

Okay this should work:

function onClick(plr)
if plr.Character:FindFirstChild("RightArm1") == nil then
	local g = script.Parent.Parent.Backpack:Clone()
	g.Parent = plr.Character
	local C = g:GetChildren()
	for i=1, #C do
		if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart" or C[i].className == "MeshPart" then
			local W = Instance.new("Weld")
			W.Part0 = g.Middle
			W.Part1 = C[i]
			local CJ = CFrame.new(g.Middle.Position)
			local C0 = g.Middle.CFrame:inverse()*CJ
			local C1 = C[i].CFrame:inverse()*CJ
			W.C0 = C0
			W.C1 = C1
			W.Parent = g.Middle
		end
	local Y = Instance.new("Weld")
	Y.Part0 = plr.Character.Torso
	Y.Part1 = g.Middle
	Y.C0 = CFrame.new(0, 0, 0)
	Y.Parent = Y.Part0
	end
	
	local h = g:GetChildren()	
	for i = 1, # h do
    	if h[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart" or C[i].className == "MeshPart" then
        	h[i].Anchored = false
        	h[i].CanCollide = false
    	end
	end
end
end
script.Parent.ClickDetector.MouseClick:connect(onClick)

This is confusing me, I click on it but nothing happens. There are no errors in output either, I have slightly edited the code.

to

if plr.Character:FindFirstChild(“Torso”) == nil then

(I notice you removed the bit at the bottom, would this not tell the script where the ClickDetector was stored?)

Yes that fine, did it work? [30chars]