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

Why cant I define character with local script!?

I have defined characters with local scripts before, but now for some reason it never works. It says character is a nil value. Please help me and thanks. Here is my scripts.

local player = game.Players.LocalPlayer
local character = player.Character
local toolclone = workspace.ClassicSword:Clone()

function onKeyPress(actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin then
			game:GetService("Chat"):Chat(player.Character.Head, "Ninja!","Black")
			character.Humanoid:EquipTool(toolclone)
		end
	end
 
game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.R)

why cant I define character in this script?

1 comment

0

on the first line, put this: repeat wait() until game:GetDescendants()

2 answers

✓ Accepted answer

The character needs to load try:

character = player.CharacterAdded:wait()

1 comment

0

oh ok thx

or this:

repeat wait() until game.Players.LocalPlayer
repeat wait() until game.Players.LocalPlayer.Character:IsDescendantOf(workspace) -- waits till the player's character is in workspace

local player = game.Players.LocalPlayer
local character = player.Character

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