Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

1 comment

0

It worked thank you! Just made another problem, now Connection2 is nil

This discussion is preserved as it was. New answers, comments, and votes are closed.