Detect Keycode Input from serverscript

1. What do you want to achieve?

– I want my script when my animation is playing and i press keybind W (must be keybind) it will do a function.

2. What is the issue? Include screenshots / videos if possible!

– server script cannot detect keybind.

local animationPlaying = true

animationTrack.Stopped:Connect(function()
	animationPlaying = false
	char:SetAttribute("Attacking", false)
	char:SetAttribute("Morph",false)

	end)
    task.spawn(function()
	while animationPlaying do
		if char:GetAttribute("Stunned") or char:GetAttribute("Ragdoll") then
		 animationTrack:Stop()

				local sound = script.Counter:Clone()
				sound.Parent = humrp
				sound:Play()
				game.Debris:AddItem(sound,1)
				
			local animationTrackK1 = animatorK:LoadAnimation(script.C1)
			animationTrackK1.Priority = Enum.AnimationPriority.Action4
			animationTrackK1.Looped = false

			local animationTrack1 = animator:LoadAnimation(script.C2)
			animationTrack1.Priority = Enum.AnimationPriority.Action4
			animationTrack1.Looped = false
			
			local dashForce = 50000
			local bodyPosition = Instance.new("BodyPosition")
			bodyPosition.MaxForce = Vector3.new(dashForce, 0, dashForce)
			bodyPosition.Position = humrp.Position + (humrp.CFrame.LookVector * -40)
			bodyPosition.P = 10000
			bodyPosition.D = 1000
			bodyPosition.Parent = humrp
			game.Debris:AddItem(bodyPosition, 0.2)
			
			animationTrack1:Play()
			animationTrackK1:Play()
			
			
			
			char:SetAttribute("Stunned",false)
			
		end
		UserInputService.InputBegan:Connect(function(input)
			if input.Keycode == Enum.Keycode.W then
				
				animationTrack:Stop()
				
				local animationTrackK2 = animatorK:LoadAnimation(script.F1)
				animationTrackK2.Priority = Enum.AnimationPriority.Action4
				animationTrackK2.Looped = false

				local animationTrack2 = animator:LoadAnimation(script.F2)
				animationTrack2.Priority = Enum.AnimationPriority.Action4
				animationTrack2.Looped = false
				
				animationTrackK2:Play()
				animationTrack2:Play()
				
				local dashForce = 50000
				local bodyPosition = Instance.new("BodyPosition")
				bodyPosition.MaxForce = Vector3.new(dashForce, 0, dashForce)
				bodyPosition.Position = humrp.Position + (humrp.CFrame.LookVector * 45)
				bodyPosition.P = 10000
				bodyPosition.D = 1000
				bodyPosition.Parent = humrp
				game.Debris:AddItem(bodyPosition, 0.2)
				
				animationTrack2.KeyframeReached:Connect(function(key)
					if key == "Hit" then
						hitbox:Start()
						wait(0.075)
						hitbox:Stop()
						hitbox:Destroy()
					end
				end)
				
			end
		end)
		task.wait(0.05)
	end
end)
  AnimationTrack:Play()

3. What solutions have you tried so far?

– I did try to look for solution but i did not find one.

simple explanation of this script is an animation plays and when animation is playing and player gets an attribute stunned then it does a thing. but if player press keybind W then it does a different thing. the problem is on the detecting if player press keybind W since this is a server script.

2 Likes

If you need to detect input, then you have to use the client and RemoteEvents.

I also wouldn’t recommend relying on animation timing for any system.

First of all you should remove the InputBegan function from the ServerScript, so the AnimationTrack.Stopped event is just the stopped function. And then create a new function (that I will be refferencing as “playWAnimation”). This function will have the code that is inside the if input.Keycode == Enum.KeyCode.W then statement. The scopng will be shown in the last step.

To start: in the ServerScript, we can create the RemoteEvent, and set a trigger that runs whenever the event is fired. This is known as “OnServerEvent”.

local RS = game:GetService("ReplicatedStorage")

local remoteEventsFolder = Instance.new("Folder")
remoteEventsFolder.Name = "RemoteEventsFolder"
remoteEventsFolder.Parent = RS

local inputWEvent = Instance.new("RemoteEvent")
inputWEvent.Name = "InputWEvent"
inputWEvent.Parent = remoteEventsFolder

local isAnimationPlaying = false

inputWEvent.OnServerEvent:Connect(function()
	
end)

Now in a LocalScript, we set up an InputBegan event that checks if the Input is “W”. Then we refference the RemoteEvent we created, and fire it to your ServerScript.

local RS = game:GetService("ReplicatedStorage")
local remoteEventsFolder = RS:WaitForChild("RemoteEventsFolder",10)
local inputWEvent = remoteEventsFolder:WaitForChild("InputWEvent",10)

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input,gameProcessedEvent)
	
	if input.KeyCode == Enum.KeyCode.W then
		
		inputWEvent:FireServer()
		
	end
	
end)

Back on the ServerScript, you can now connect the OnServerEvent to your animation function.

local RS = game:GetService("ReplicatedStorage")

local remoteEventsFolder = Instance.new("Folder")
remoteEventsFolder.Name = "RemoteEventsFolder"
remoteEventsFolder.Parent = RS

local inputWEvent = Instance.new("RemoteEvent")
inputWEvent.Name = "InputWEvent"
inputWEvent.Parent = remoteEventsFolder

local isAnimationPlaying = false

local function playWAnimation()
	
	print("running function")
	
end

inputWEvent.OnServerEvent:Connect(function()
	
	if isAnimationPlaying == true then
		
		playWAnimation()
		
	end
	
end)
1 Like

Thanks for the reply. It wasnt really what i wanted it to be. But i found a simpler way similiar to yours