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

attempt to index global 'c' (a nil value). Why?

Asked by 4 years ago

I wrote a script that gives the player a surrounding shield whenever they press a certain key. However, when I load the game (both in Studio and on Roblox) it says "Players.Player1.Backpack.Shield:3: attempt to index global 'c' (a nil value)

Here it is:

p = game.Players.LocalPlayer
c = p.Character
hum = c:WaitForChild('Humanoid')
pressed = false
cooldown = 3
utilitytime = 10
show = p.PlayerGui:WaitForChild('Powers').Shield
local a = hum:LoadAnimation(script.Animation)

s = Instance.new('Part')
sw = Instance.new('Weld',s)

function onKeyPress(Shield, userInputState, inputObject)
    if userInputState == Enum.UserInputState.Begin then
        if not pressed then
        pressed = true
        print('Q was pressed')
        a:Play()
        s.Parent = workspace
        s.Shape = 'Ball'
        s.Locked = false
        s.Anchored = false
        s.Transparency = 0.5
        s.CanCollide = false
        s.BrickColor = BrickColor.new('Cyan')
        s.Material = 'Neon'
        s.Size = Vector3.new(6,6.2,6)
        s.CFrame = p.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,0)

        sw.Part0 = p.Character.HumanoidRootPart
        sw.Part1 = s
        sw.C0 = CFrame.new(0,0,0)
        sw.Parent = s

        show.BackgroundColor3 = Color3.new(0,0,0)
        show.Text = 'SHIELD: ON USE'
        end
    end
    wait(utilitytime) show.Text = 'SHIELD: ON USE'
    wait(.4)
    s.BrickColor = BrickColor.new('Deep orange')
    wait(.4)
    s.BrickColor = BrickColor.new('Bright orange')
    wait(.4)
    s.BrickColor = BrickColor.new('Really red')
    wait(.4)
    s:remove()
    sw:remove()
    show.Text = 'SHIELD: COOLDOWN'
    wait(cooldown)
    show.Text = 'SHIELD (Q)'
    show.BackgroundColor3 = Color3.new(0,0,0)
    pressed = false
end
game.ContextActionService:BindAction('keyPress',onKeyPress,false,Enum.KeyCode.Q)

Why's this happening? I searched online for similar problems, yet I couldn't find much help. And this script is a local script, placed in the starter pack.

2 answers

Log in to vote
0
Answered by 4 years ago

It's most most likely because you're not naming your variables right. You should be using local variables.Try replacing your variables with this:

local p = game.Players.LocalPlayer
local c = p.Character or p.CharacterAdded:Wait() -- sometimes the script can load before the player
local hum = c:WaitForChild('Humanoid')
local pressed = false
local cooldown = 3
local utilitytime = 10
local show = p.PlayerGui:WaitForChild('Powers').Shield
local a = hum:LoadAnimation(script.Animation)

local s = Instance.new('Part')
local sw = Instance.new('Weld',s)
0
Thank you so much. I'd give you a kiss if I could ;) luicasas 13 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The problem is that the game isn’t loading the character fast enough, therefor resulting in a nil value. To fix this problem, change it to this;

c = plr.Character or p.CharacterAdded:Wait()

Else add a wait(3) in the beginning.

Also, here is a tip; Make your variables better(don’t just use c or p, make it more clear like plr or ch)

Answer this question