On this script, it says that char is a nil value and it breaks the script, even though it works in test mode. Any idea why it's doing this?
local Player = game.Players.LocalPlayer local mouse = Player:GetMouse() local lastUse = 0; local char = Player.Character local hum = char.Humanoid local function onKeyDown(key) local Key = key:lower() if key == "z" and Player.Levelss.Level.Value >= 15 and tick()-lastUse > 10 and Player.Levelss.RangedPunch.Value < 1 then local x = Instance.new("Part") x.BrickColor = BrickColor.new("White") x.Material = "Neon" x.CanCollide = false x.Transparency = 0.6 x.Size = Vector3.new(10,10,10) x.TopSurface = "Smooth" x.BottomSurface = "Smooth" x.Shape = "Ball" x.Name = Player.Name local fd = script.fireDamage:clone() fd.Parent = x local a = Instance.new("Sound") a.SoundId = "http://www.roblox.com/asset/?id=260430117" a.Parent = x a.Volume = 2 a:play() Player.Levelss.XP.Value = Player.Levelss.XP.Value + 8 local y = Instance.new("BodyVelocity") y.maxForce = Vector3.new(math.huge ,math.huge ,math.huge) y.velocity = Player.Character.Torso.CFrame.lookVector*95 x.CFrame = Player.Character.Torso.CFrame*CFrame.new(0, 1 , -10) x.Parent = Workspace y.Parent = x game.Debris:AddItem(x, 6) lastUse = tick(); end end mouse.KeyDown:connect(onKeyDown)
The character doesn't load instantly after the player sometimes, so you have to wait for it to load. Use this method:
local char = plr.Character or plr.CharacterAdded:Wait()
That way you know char variable isn't ever going to be nil.
Also, I suggest using WaitForChild
for the Humanoid
so you know that's not nil as well.
local Player = game.Players.LocalPlayer local char = Player.Character or Player.CharacterAdded:Wait() local Humanoid = Char:WaitForChild("Humanoid")
Another thing I highly suggest is NOT using KeyDown
. Use UserInputService
instead.
local Player = game.Players.LocalPlayer local char = Player.Character or Player.CharacterAdded:Wait() local hum= char:WaitForChild("Humanoid") local lastUse = 0; local function onKeyDown(inputObject) local key = inputObject.KeyCode if key == Enum.KeyCode.Z and Player.Levelss.Level.Value >= 15 and tick()-lastUse > 10 and Player.Levelss.RangedPunch.Value < 1 then local x = Instance.new("Part") x.BrickColor = BrickColor.new("White") x.Material = "Neon" x.CanCollide = false x.Transparency = 0.6 x.Size = Vector3.new(10,10,10) x.TopSurface = "Smooth" x.BottomSurface = "Smooth" x.Shape = "Ball" x.Name = Player.Name local fd = script.fireDamage:clone() fd.Parent = x local a = Instance.new("Sound") a.SoundId = "http://www.roblox.com/asset/?id=260430117" a.Parent = x a.Volume = 2 a:play() Player.Levelss.XP.Value = Player.Levelss.XP.Value + 8 local y = Instance.new("BodyVelocity") y.maxForce = Vector3.new(math.huge ,math.huge ,math.huge) y.velocity = Player.Character.Torso.CFrame.lookVector*95 x.CFrame = Player.Character.Torso.CFrame*CFrame.new(0, 1 , -10) x.Parent = Workspace y.Parent = x game.Debris:AddItem(x, 6) lastUse = tick(); end end game:GetService("UserInputService").InputBegan:Connect(onKeyDown)