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. My problem is, that the first time the player fires this function, the if statement inside the KeyframeReached function doesn't activate. This only happens for the FIRST time the remoteevent is fired. Why is this?

Code:

function GomuGomuPistol(PlayerWhoSent, KeyHeld, MousePosition)
	local Character = PlayerWhoSent.Character
	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 Damaging = false
	local Attacking = false
	local Damage = 25
	local Range = 25
	local BodyGyro = Instance.new('BodyGyro')
	local BodyPosition = Instance.new('BodyPosition')
	local ArmStretch = Instance.new('Part')
	
	if KeyHeld then
		print('keyheld')
		GomuPistolAnimTrack = Humanoid:LoadAnimation(GomuPistolAnim)
		GomuPistolAnimTrack:Play()
		GomuPistolAnimTrack.KeyframeReached:Connect(function(Keyframe)
			if Keyframe == 'Charge' and KeyHeld then
				GomuPistolAnimTrack:AdjustSpeed(0)
				print('pistolcharge')
				Attacking = true
			end
		end)
	elseif not KeyHeld then
		print('notkeyheld')
		GomuPistolAnimTrack:AdjustSpeed(1)
		Humanoid.WalkSpeed = 0
		print('pistol')
		wait(5)
	end
	Humanoid.WalkSpeed = 16

2 comments

1

You shouldn't nest events inside functions as they are asynchronous, make line 20 KeyframeReached:Wait() or take it out the function.

0

Alright so I took it out of the function, and just called it using GomuPistolAnimTrack.KeyframeReached:Connect(ReachingKeyframe) and it works! Thank you, you are very helpful. You always comment on my questions.