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

Why is character a nil value if it works without an error in another local script?.

I am stuck on why this code is nil when it works perfectly fine on another local script? All of these scripts are in StarterPlayerScripts. The error message is Players.Zeppelin0330.PlayerScripts.LocalScript:1: attempt to index field 'Character' (a nil value)

if game.Players.LocalPlayer.Character.Beep.Value == 0 then -- this line states that character is a nil value
		game.Players.LocalPlayer.PlayerGui.ScreenGui.Dave.Grenade.Text = "x 2"
		end
game.Players.LocalPlayer.Character.Beep.Changed:Connect(function()
	print("why?")
	
		if game.Players.LocalPlayer.Character.Beep.Value == 1 then
	game.Players.LocalPlayer.PlayerGui.ScreenGui.Dave.Grenade.Text = "x 1"
	 else if game.Players.LocalPlayer.Character.Beep.Value == 0 then
		game.Players.LocalPlayer.PlayerGui.ScreenGui.Dave.Grenade.Text = "x 0"

	end
		end
		end)

this is the working one and it is similar to the one above but works?!

game.StarterGui.ScreenGui.Dave.Visible = true

local throwPower = 100
local debounce = true
local canThrow = true
local player = game.Players.LocalPlayer
game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(KeyPressed)
	if KeyPressed == "g" and debounce == true and player.Character.Beep.Value < 2 then
		player.Character.Beep.Value = player.Character.Beep.Value + 1
		print("it works")
			if player.Character.Team.Value == 2 then
		debounce = false
		print("hi")
		local Back = player.Backpack
		game.Workspace.StickGrenade:Clone().Parent = player.Backpack
		player.Character.Humanoid:EquipTool(Back.StickGrenade)
		player.Character.Humanoid.Died:Connect (function()
			
		end)
		end
		wait(1.5)
		end
	      
	wait(2)
	debounce = true
end)
		
	
		local player = game.Players.LocalPlayer
game.Workspace.Meep.Why.Changed:Connect(function()
	print("hi")
	game.Players.LocalPlayer.PlayerGui.ScreenGui.Dave.Visible = true
	
end)

3 comments

0

whats the exact error message?

0

and which line is the error coming from?

0

I state that line 1 is the error line and the error it states is Players.Zeppelin0330.PlayerScripts.LocalScript:1: attempt to index field 'Character' (a nil value)

2 answers

✓ Accepted answer

You are getting that error because the local script is being ran before the player's character exists. You need to first wait for the character to exist before you continue your code. This can be done in various ways (WaitForChild is not applicable here because Character is not a direct descendant of Player and is considered a 'property').

One common way to do this, which the other answer utilizes, is:

repeat wait() until Player.Character

This will yield the script until Character is a property of Player.

game.Players.PlayerAdded:Connect(function(plr)
repeat wait() until plr.Character
if plr.Character.Beep.Value == 0 then
        plr.PlayerGui.ScreenGui.Dave.Grenade.Text = "x 2"
        end
plr.Character.Beep.Changed:Connect(function()
    print("why?") 

        if plr.Character.Beep.Value == 1 then
    plr.PlayerGui.ScreenGui.Dave.Grenade.Text = "x 1"
     else if plr.Character.Beep.Value == 0 then
        plr.PlayerGui.ScreenGui.Dave.Grenade.Text = "x 0"

    end
        end
        end)
end)

yes i know i'm a bad scripter

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