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

Why do i keep getting the attempt to index nil with 'Character' with my script?

Im trying to make an attack on titan game and I want to make the titan shifter power first, I scripted it so when they press F they change into a titan, I already scripted it so the camera would stick to the titan but the body wont move so i made it so the character changes into the titan but i am getting an error that im trying to index nil with Character which i said like 'local char = plr.Character' I dont know why im getting an error, I clearly described plr as game.Players.LocalPlayer so why am i getting this error! am i not suppose to do this script in a server script and a local script? I tried that already in a local script i cant use OnServerEvent. BTW This is both the scripts right here :

Server Script :

--Variables
local event = game.ReplicatedStorage.Remotes.BeginTitanShift
local plr = game.Players.LocalPlayer
local char = plr.Character
local rig = game.Workspace.PlayerShifterTitan

--Code


event.OnServerEvent:Connect(function(shift)
	local camera = workspace.CurrentCamera
	camera.CameraType = "Follow"
	camera.CameraSubject = rig.HumanoidRootPart
	script.Parent.RemoteEvent:FireServer()
        char = rig
end)

Local Script(In StarterCharacterScripts) :

--Variables
local key = Enum.KeyCode.F
local uis = game:GetService("UserInputService")
local event = game.ReplicatedStorage.Remotes.BeginTitanShift
local player = game.Players.LocalPlayer




--Code

uis.InputBegan:Connect(function(input, g)
	if g then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == key or Enum.KeyCode.F then
			event:FireServer()
		end
	end
end)

If you know the error tell me plzzzzz

1 comment

0

Attempt to index nil with something means; that the instance/thing you're trying to reference, does not exist! Also, localplayer does not work with the server. And when using remoteevents the first parameter MUST always be a player.

2 answers

✓ Accepted answer

LocalPlayer only works on LocalScripts.

So, you can do:

--Variables
local event = game.ReplicatedStorage.Remotes.BeginTitanShift
local rig = game.Workspace.PlayerShifterTitan

--Code


event.OnServerEvent:Connect(function(plr, shift)
local char = plr.Character
    local camera = workspace.CurrentCamera
    camera.CameraType = "Follow"
    camera.CameraSubject = rig.HumanoidRootPart
    script.Parent.RemoteEvent:FireServer()
        char = rig
end)

3 comments

0

what bout the plr you legit just removed plr so now it will error

0

@TheQuitzzyKadren u cannot use LocalPlayer in ServerScripts, and while receiving events from local script, a variable has to be assigned for plr who fired it.

0

TheQuit, I did not.

Try this


--Variables
local event = game.ReplicatedStorage.Remotes.BeginTitanShift
local plr = game.Players.LocalPlayer
repeat wait() until plr.Character 
local char = plr.Character
local rig = game.Workspace.PlayerShifterTitan

--Code


event.OnServerEvent:Connect(function(shift)
    local camera = workspace.CurrentCamera
    camera.CameraType = "Follow"
    camera.CameraSubject = rig.HumanoidRootPart
    script.Parent.RemoteEvent:FireServer()
        char.PrimaryPart = rig.PrimaryPart ----Changed it---
end)

4 comments

0

This will still error.

0

Try do do on line 16 char.PrimaryPart = rig

0

Also put it in a local script i forgot to mention that if the Char.PrimaryPart = rig.PrimaryPart does not work then do Char = rig like before

1

This is still incorrect; you can't reference the localplayer in a server script, and also, :OnServerEvent must always have player attached to it!

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