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.

1 comment
Thank you so much. I'd give you a kiss if I could ;)