I have a LocalScript inside the player(s), which records if a player presses, and releases a certain key, which in this case, is Z. Pressing the key fires the GomuPistolEvent and KeyHeld = true, releasing the key fires the GomuPistolEvent and KeyHeld = false. I recently had a problem, which was quickly fixed, but now I get an error at line 64: ServerScriptService.DevilFruitScripts.GomuGomu.GomuGomu:64: attempt to index local 'BodyGyro' (a nil value)
I have tried to assign the "BodyGyro" variable as an empty variable without nil, but that doesn't seem to fix it. How would I go about fixing this?
Code:
--//Services local UserInputService = game:GetService('UserInputService') local ReplicatedStorage = game:GetService('ReplicatedStorage') local ServerStorage = game:GetService('ServerStorage') local ServerScriptService = game:GetService('ServerScriptService') local Players = game:GetService('Players') local GomuRemotes = ReplicatedStorage.Remotes.DevilFruits:WaitForChild('GomuGomu') --//Variables local GomuPistolEvent = GomuRemotes.GomuPistol:WaitForChild('GomuPistolEvent') local DamageModule = require(ServerStorage.Server.DamageService) --//Animations local GomuPistolAnim = Instance.new('Animation') GomuPistolAnim.AnimationId = 'rbxassetid://2188557970' GomuPistolAnimTrack = nil --//Coding GomuPistolEvent.OnServerEvent:Connect(function(PlayerWhoSent, KeyHeld) local Character = game.Workspace:WaitForChild(PlayerWhoSent.Name) local Humanoid = Character:FindFirstChildOfClass('Humanoid') local RightArm = Character:WaitForChild('RightLowerArm') local RightHand = Character:WaitForChild('RightHand') local LeftArm = Character:WaitForChild('LeftLowerArm') local LeftHand = Character:WaitForChild('LeftHand') local Mouse = PlayerWhoSent:GetMouse() local MousePosition = Mouse.Hit.p local Damaging = false local Attacking = false local Damage = nil local Cooldown = nil local Range = nil local BodyGyro = nil local BodyPosition = nil local ArmStretch = nil local Weld = nil if KeyHeld then GomuPistolAnimTrack = Humanoid:LoadAnimation(GomuPistolAnim) GomuPistolAnimTrack:Play() GomuPistolAnimTrack.KeyframeReached:Connect(function(Keyframe) if KeyHeld and Keyframe == 'Charge' then GomuPistolAnimTrack:AdjustSpeed(0) end end) print('pistolcharge') Attacking = true BodyGyro = Instance.new('BodyGyro', Character.LowerTorso) BodyGyro.Name = 'SkillGyro' BodyGyro.D = 100 BodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge) BodyPosition = Instance.new('BodyPosition', Character.LowerTorso) BodyPosition.Name = 'SkillPosition' BodyPosition.Position = Character.HumanoidRootPart.Position BodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge) while KeyHeld do MousePosition = Mouse.Hit.p BodyGyro.CFrame = CFrame.new(Character.LowerTorso.Position, MousePosition) * CFrame.Angles(0, math.rad(-90) ,0) wait() end elseif not KeyHeld then GomuPistolAnimTrack:AdjustSpeed(1) print('pistol') BodyGyro.CFrame = CFrame.new(Character.LowerTorso.Position, MousePosition) GomuPistolAnimTrack.KeyframeReached:Connect(function(Keyframe) if Keyframe == 'Pistol' then Damaging = true Damage = 25 ArmStretch = Instance.new('Part', Character) ArmStretch.Name = 'ArmStretchRight' ArmStretch.Color = RightArm.Color ArmStretch.Size = RightArm.Size ArmStretch.CanCollide = false RightArm.Transparency = 1 RightHand.Transparency = 1 Weld = Instance.new('Weld', ArmStretch) Weld.Part0 = ArmStretch Weld.Part1 = RightArm Weld.C0 = CFrame.new(0, 0, 0) * CFrame.new(0, 0, 0) end if Keyframe == 'End' then Character.LowerTorso:WaitForChild('SkillGyro'):Destroy() Character.LowerTorso:WaitForChild('SkillPosition'):Destroy() Character:WaitForChild('ArmStretchRight'):Destroy() if RightArm.Transparency == 1 then RightArm.Transparency = 0 end if RightHand.Transparency == 1 then RightHand.Transparency = 0 end BodyGyro = nil BodyPosition = nil ArmStretch = nil Weld = nil Attacking = false end end) end RightArm.Touched:Connect(function(hitPart) if Attacking and hitPart.Parent:FindFirstChild('Humanoid') and Damaging then Damaging = false local HumanoidToDamage = hitPart.Parent:FindFirstChild('Humanoid') DamageModule.Combat(HumanoidToDamage, Damage) end end) LeftArm.Touched:Connect(function(hitPart) if Attacking and hitPart.Parent:FindFirstChild('Humanoid') and Damaging then Damaging = false local HumanoidToDamage = hitPart.Parent:FindFirstChild('Humanoid') DamageModule.Combat(HumanoidToDamage, Damage) end end) end)
Since the if
statement returns false
, the BodyGyro
is never created with Instance.new
since it is created only if the if
statement returns true
. As a result, it will always be nil
and it will error.
You can either create a BodyGyro
on the else
before you use it or create a single one before the if
loops.
Thanks for reading my answer. If I helped, remember to vote up this answer and mark it as answered if I was correct.
~Undo