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

I'm attempting to script an ability system, but this won't work?

local stats = script.Parent.Parent:WaitForChild("statistics")
print("stats")
local ability = game.Players.LocalPlayer:WaitForChild("ability")
print("ability")
local replicatedstorage = game:GetService("ReplicatedStorage")
print("replicatedstorage")
local statustag = replicatedstorage:WaitForChild("statustag")
print("statustag")
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
print("humanoid")
local using = Instance.new("BoolValue")
using.Parent = game.Players.LocalPlayer
using.Name = "using"
using.Value = false
ability.Value = "fast"
function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.E and using.Value == false and ability.Value == "fast" and statustag.Value == "Game in progress" then
		using.Value = true
		print("E pressed.")
			repeat wait() until humanoid ~= nil
				script.Sound:Play()
				humanoid.WalkSpeed = humanoid.WalkSpeed + 20
				wait(5)
				humanoid.WalkSpeed = humanoid.WalkSpeed - 20
				using.Value = false
		end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

When the player presses 'e,' their walkspeed is supposed to go up by 20.

I get the following error: Line 11: Attempt to index local 'character,' a nil value.

I don't quite understand how it is nil, as I defined it above.

2 answers

✓ Accepted answer

Sometimes the script will run before the Character is loaded. This makes your character variable equal nil. To get around this, you must use one of the many ways to wait for the character to load until defining it, my favorite being:

local plr = game.Players.LocalPlayer
	repeat wait() until plr.Character
local chr = plr.Character

Best to use this instead of a repeat

local plr = game.Players.LocalPlayer
plr.Character:WaitForChild('Humanoid')
local chr = plr.Character
--spyspace12

1 comment

0

The Humanoid will never be in the Player, but your right that the repeat loop on line 22 is flawed.

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