Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago
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

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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
Ad
Log in to vote
0
Answered by 9 years ago

Best to use this instead of a repeat

local plr = game.Players.LocalPlayer
plr.Character:WaitForChild('Humanoid')
local chr = plr.Character
--spyspace12
0
The Humanoid will never be in the Player, but your right that the repeat loop on line 22 is flawed. Perci1 4988 — 9y

Answer this question