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

Help with local scripts?

Ok,so I decided to make a script of mine into a local script and it seems to act odd. It fails at making the welds and destroying the left arm at death in solo.Every time I start a sever with 2 players it get an error saying:

Players.Player1.PlayerGui.LocalScript:10: attempt to index global 'Character' (a nil value)

which I don't get because I have a variable called Character at line 2.Oh,and this seems to work find when I put it in a regular script,why and how do I fix all these problems?

LocalScript:

Player = game.Players.LocalPlayer
   local Character = Player.Character 
	if not Character or Character.Parent == nil 
		then Character = Player.CharacterAdded:wait()
---------------------------------------------------------
        Player.CameraMode= Enum.CameraMode.LockFirstPerson
---------------------------------------------------------
            function MakeDisplayArms()
---------------------------------------------------------
                local CurrentCamera = game.Workspace.CurrentCamera
                local PlayerArms = {
                  Character:WaitForChild("Right Arm"),
                   Character:WaitForChild("Left Arm")}
                local ClonedArms = {}
---------------------------------------------------------
                for i,v in pairs (PlayerArms) do
                    wait()
                    local Clone = v:Clone()
                    Clone.Name = "Cloned_"..v.Name --Renames it
                    Clone.CanCollide = true
                    local Weld = Instance.new("Weld",Clone)
                    Weld.Part0 = Clone
                    Weld.Part1 = v
                    Clone.Parent = CurrentCamera--Hides it from everyone else
                    ClonedArms[i] = Clone
                end
                Character.Humanoid.Died:connect(function()
				wait()
                    for Index,Parts in pairs (ClonedArms)do
                        print(Parts.Name)
                        Parts:Destroy()
                        table.remove(ClonedArms,Index)
                        Parts:BreakJoints()
                    end
                end)
				end
                return ClonedArms
            end
---------------------------------------------------------
MakeDisplayArms()   


If you want to test it in place it's here.

It's so hard working with local scripts. qq

1 answer

✓ Accepted answer

LocalScripts often run before the character has been created.

Change line 2 to:

Character = Player.Character or Player.CharacterAdded:wait()

If Player.Character is nil, like in your case, then it will wait until the CharacterAdded event fires. The CharacterAdded event returns the character object, so it sets Character to that new object.

Edit: Since the reply chain was getting too long, I'll reply with some code changes:

  1. Fixed the indentation. Typically you only indent inside a function.
  2. When you spawn, it's possible that Player.Character is a reference to the old Character object, which was parented to nil. If that's the case, you need to wait for the CharacterAdded event to fire before proceeding. I think this was the root of your problem. This is definitely confusing behavior, I filed a bug in February about it and haven't gotten a response from the admins.
  3. I recommend clearing the CurrentCamera's children each time you die to get rid of fake arms that might have slipped past.

I tested this code in online mode and it seems to work. Let me know if you have any more questions.

Player = game.Players.LocalPlayer
local Character = Player.Character 
if not Character or Character.Parent == nil then
	Character = Player.CharacterAdded:wait()
end

Player.CameraMode= Enum.CameraMode.LockFirstPerson

function MakeDisplayArms()
	local CurrentCamera = game.Workspace.CurrentCamera
	local PlayerArms = {
	Character:WaitForChild("Right Arm"),
	Character:WaitForChild("Left Arm")
	}
	local ClonedArms = {}
	for i,v in pairs (PlayerArms) do
		wait()
		local Clone = v:Clone()
		Clone.Name = "Cloned_"..v.Name --Renames it
		print(Clone.Name .. " created")
		Clone.CanCollide = true
		local Weld = Instance.new("Weld",Clone)
		Weld.Part0 = Clone
		Weld.Part1 = v
		Clone.Parent = CurrentCamera--Hides it from everyone else
		ClonedArms[i] = Clone
	end
	Character:WaitForChild("Humanoid").Died:connect(function()
		wait()
		print("Died")
		CurrentCamera:ClearAllChildren()
	end)
end

MakeDisplayArms()   

20 comments

0

Sorry to bother you, but it only solved some of it.The welds aren't working still,is there anyway I can fix it?I also got a new error (I also updated the script) Right Arm is not a valid member of Model

1

It's possible that the Right Arm hasn't been created yet. On line 10 and 11, if the arms don't exist, it will throw an error because you're trying to index them.

1

I recommend changing lines 10 and 11 to use WaitForChild too. e.g. Character:WaitForChild("Right Arm")

0

I changed it and works it just won't remove on death and then the arms won't spawn until I reset again then the arms come out when I reset but then won't get destroyed.

1

Is there any output? Press F9 and click on Local Console. Look for red errors.

0

think because it won't wait for the character,but how would I make it do that if it's in the loop already?

0

No errors in studio while starting a server nor in Online

1

On line 26, try adding: print(Parts.Name) Parts:Destroy()

0

It printed "Cloned_Right Arm" at the first time I reseted,then It didn't print at all at the second reset(I updated script again).

1

Looks like the arms are falling to the ground. Don't parent them to CurrentCamera until after the weld is created.

0

It fixed the problem in solo but it still doesn't work in a server and I parented it in line 22

1

I think the problem may be that when the Localscript first runs, Player.Character still points to the old character model.

1

Try changing line 2 to this: local Character = Player.Character if not Character or Character.Parent == nil then Character = Player.CharacterAdded:wait() end

0

It didn't do anything and what do you mean that is still points to the old model,the Character changes when a player enters?

1

After your character dies, local scripts may run before the new character respawns. In that case it would be cloning the old limbs.

0

Probably if I edit something within the Died event it fix it.I'll add a wait.

0

That didn't work,maybe if I try to compare the Character when he spawns with the character after?I have to go for now,bye and thanks for the help for today.

0

It won't lock the player in first person mode but it will change the property.I tried using "WaitForChild("Camera") but it didn't work,why?

1

So the arms part of the script works, just not the camera?

0

yes

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