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

I can't figure out why this isn't working..?

Asked by 8 years ago

I have this animation script changer when you press ctrl and it gives me this error in the output: 01:26:05.945 - Players.Player.PlayerGui.LocalScript:62: attempt to index global 'Connection1' (a nil value) 01:26:05.946 - Stack Begin 01:26:05.946 - Script 'Players.Player.PlayerGui.LocalScript', Line 62 01:26:05.947 - Stack End

And here's the scsript:

`local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local debounce = false

Mouse.KeyDown:connect(function(Key)
if Key:byte() == 50 then
for i = 1,20 do
workspace.Camera.FieldOfView = workspace.Camera.FieldOfView + .4
Player.Character.Humanoid.WalkSpeed = Player.Character.Humanoid.WalkSpeed + .9
wait(.001)
end
AnimateDirectory=script.Parent.Parent:WaitForChild("Animate")
    if AnimateDirectory~=nil then
        AnimateDirectory.Disabled=true
    end
    Load1=Player.Character:FindFirstChild("Humanoid"):LoadAnimation(script.Idle)
    Load2=Player.Character:FindFirstChild("Humanoid"):LoadAnimation(script.Run)
    Load3=Player.Character:FindFirstChild("Humanoid"):LoadAnimation(script.Jump)

    Load1:Play()

    Connection1=Player.Character:FindFirstChild("Humanoid").Running:connect(function(Speed)
        if Speed>=0.5 then
        Load1:Stop(0.1)
        Load3:Stop(0.1)
        if Load2.IsPlaying==false then
        Load2:Play(0.1)
        print("Humanoid Ran!")
        end
        else
        Load2:Stop(0.1)
        Load3:Stop(0.1)
        if Load1.IsPlaying==false then
        Load1:Play(0.1)
        print("Humanoid Idled!")
        end
        end
    end)

    Connection2=Player.Character:FindFirstChild("Humanoid").Jumping:connect(function()
        Load1:Stop(0.1)
        Load2:Stop(0.1)
        Load3:Play(0.1)
        print("Humanoid Jumped!")
    end)
workspace.Camera.FieldOfView = 80
Player.Character.Humanoid.WalkSpeed = 30
end
end)

Mouse.KeyUp:connect(function(Key)
if Key:byte() == 50 then
for i = 1,20 do
workspace.Camera.FieldOfView = workspace.Camera.FieldOfView - .4
Player.Character.Humanoid.WalkSpeed = Player.Character.Humanoid.WalkSpeed - .9
wait(.001)
end
    if AnimateDirectory~=nil then
        AnimateDirectory.Disabled=false
    end

    Connection1:disconnect()
    Connection2:disconnect()
    Load1:Stop()
    Load2:Stop()
    Load3:Stop()
workspace.Camera.FieldOfView = 70
Player.Character.Humanoid.WalkSpeed = 16
end
end)
`

Please help :(

1 answer

Log in to vote
0
Answered by 8 years ago

It looks to be like you're somehow managing to capture a KeyUp before a keydown. Try nil checking Connection1 before trying to disconnect it (And similarly for Connection2)

if Connection1 then Connection1:disconnect() end;
if Connection2 then Connection2:disconnect() end;

An alternative is to use UIS instead of the deprecated Mouse events for KeyDown/KeyUp

0
It worked thank you! Just made another problem, now Connection2 is nil NICCO890 35 — 8y
Ad

Answer this question