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

Script is giving me attempt to index local (a nil value), help?

Asked by
Phyb 28
5 years ago

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)
0
UserInputService and Mouse are client-side only, and the parent argument to Instance.new is deprecated. User#19524 175 — 5y
0
So I would have to get the Player's Mouse through my LocalScript rather than in my ServerScript? Phyb 28 — 5y
0
Yes, same for UserInputService. Pass the Hit of the Mouse as a parameter and from the client send the Hit. User#19524 175 — 5y
0
I still get the same error "attempt to index local 'BodyGyro' (a nil value)" Phyb 28 — 5y
0
Also, if I pass the Hit as a parameter from the client, the code during the while loop at line 56, which is supposed to rotate the player while KeyHeld = true using Mouse.Hit.p, doesn't rotate the player anymore. Phyb 28 — 5y

1 answer

Log in to vote
0
Answered by
xAtom_ik 574 Moderation Voter
5 years ago

It's simple.

BodyGyro will always be nil.

The cause?

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.

The fix?

You can either create a BodyGyro on the else before you use it or create a single one before the if loops.

Conclusion

Thanks for reading my answer. If I helped, remember to vote up this answer and mark it as answered if I was correct.

~Undo

0
Thank you so much, I don't quite understand it, but I followed what you said. All I have to do is change a few things, I would upvote but I need another 13 Reputation. Phyb 28 — 5y
Ad

Answer this question