Function not firing after pressing the respective button

I’ve tried making a dodge mechanic with other combat mechanics in the works, but the problem is the function just doesn’t want to work I’ve tried all I can but still can’t find a solution to this

-- Valid Keybinds
local ValidKeys = {
	["Q"] = Dodge
}

-- Event Handler
InputSender.OnServerEvent:Connect(function(plr, keyName)
	local action = ValidKeys[keyName]
	if action then
		print("Valid input received from: " .. plr.Name)
		if plr:GetAttribute("CanDodge") and keyName == "Q" then
			RS:WaitForChild("DodgeRemote").OnServerEvent:Connect(function(plr, MoveDir, LookVector)
			Dodge(plr, MoveDir, LookVector)
			end)
		else
			print(plr.Name .. " is on dodge cooldown")
		end
	else
		print("Invalid input received from: " .. plr.Name)
	end
end)

This is the part that’s supposed to work everything functions well other than the part where the dodge commences I’ve checked the dodge function and everything works fine there so I don’t know why this is happening, any ideas or fixes?

1 Like

Where exactly is it failing? Is it printing Valid input recieved? Have you tried adding more print statements to check if the other if statements pass? Is the function recieving the right intputs with right type?

print("Valid input received from: " .. plr.Name)
		if plr:GetAttribute("CanDodge") and keyName == "Q" then
			RS:WaitForChild("DodgeRemote").OnServerEvent:Connect(function(plr, MoveDir, LookVector)
			Dodge(plr, MoveDir, LookVector)
			end)

Pretty sure this is where it fails the other keybinds work fine and I presume the
“if action then” statement or the server firing doesn’t work if you need the part that triggers the script I’ll send it in the next message since I’m not in studio right now.

And yes I have double-checked everything it all seems fine even inquired an AI to no avail.

Sending the rest of the code is most likely necessarry, but Ill try my best without, altough it isnt easy to understand what you are trying to do.

From what I see, you must be detecting inputs on the client somewhere else, then sending those inputs over to the server through a RemoteEvent. From there I guess you perform some checks on the server for whether the player should be able to dodge and then you send a signal back to the client to perform the dodge. In theory this would work, although it isnt ideal, will lag and does not prevent them from cheating in any way and could simply just be done on the client side. (Preventing cheating has to then be done with an anti-cheat that detects suspicious movement since the client has full server-side control over the character anyway.)

Where the issue occurs is most likely here:

RS:WaitForChild("DodgeRemote").OnServerEvent:Connect(function(plr, MoveDir, LookVector)

You receive the signal from the server with all the inputs, then you check if the player wants to/is able to dodge, but then you basically do nothing except waiting until another signal comes from a totally different RemoteEvent. At some point, if you do fire this event, it should dodge, but Im not sure if you even use the DodgeRemote event at all?

Its really hard for me to grasp what you are trying to do, but if everything else is as I expect it, doing this instead should fix it:

local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")

-- Valid Keybinds
local ValidKeys = {
	["Q"] = Dodge
}

InputSender.OnServerEvent:Connect(function(plr, keyName)
	local action = ValidKeys[keyName]
	if action then
		print("Valid input received from: " .. plr.Name)
		if plr:GetAttribute("CanDodge") and keyName == "Q" then
			Dodge(plr, Humanoid.MoveDirection, HumanoidRootPart.CFrame.LookVector)
		else
			print(plr.Name .. " is on dodge cooldown")
		end
	else
		print("Invalid input received from: " .. plr.Name)
	end
end)

I dont know for sure what the rest of your code looks like so this is just a guess.

This is the local script that manages the remote events.

-- Services
local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

-- Variables
local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local IS = RS:WaitForChild("InputSender")
local DR = RS:WaitForChild("DodgeRemote")

UIS.InputBegan:Connect(function(input)
	local IKCN = input.KeyCode.Name
	if IKCN == "Unknown" then
		--print("Unknown input, returning")
		return
	else
		if IKCN == "Q" then
			--print(chr:WaitForChild("Humanoid").MoveDirection)
			--print(chr:WaitForChild("HumanoidRootPart").CFrame.LookVector)
			DR:FireServer(IKCN, chr:WaitForChild("Humanoid").MoveDirection, chr:WaitForChild("HumanoidRootPart").CFrame.LookVector)
			else
		--print("Firing combatserver for " .. IKCN)
		IS:FireServer(IKCN)
		end
	end
end)

The dodge function in the server script.

local function Dodge(plr, MoveDir, LookVec)
	-- DODGE SETTINGS
	local SpeedMult = 150
	local SteerInterval = 0.05
	local DodgingTime = 0.5
	local CooldownTime = 1

	-- Check cooldown
	if not plr:GetAttribute("CanDodge") then
		print(plr.Name .. " is on dodge cooldown")
		return
	end

	-- Finding Character
	local chr = plr.Character
	if not chr then
		wait(1)
		return
	end
	local hm = chr:WaitForChild("Humanoid")
	local hrp = chr:FindFirstChild("HumanoidRootPart")
	if not (hm and hrp) then return end
	print("Character found for " .. plr.Name)

	-- Validate MoveDir and LookVec
	MoveDir = MoveDir and MoveDir.Magnitude > 0 and MoveDir.Unit or Vector3.new(0, 0, 0)
	LookVec = LookVec and LookVec.Unit or hrp.CFrame.LookVector

	-- Set attributes
	plr:SetAttribute("CanDodge", false)
	hm:SetAttribute("IsDodging", true)
	hm:SetAttribute("HasIFrames", true)

	-- Create Attachment
	local attachment = Instance.new("Attachment")
	attachment.Parent = hrp

	-- Set up LinearVelocity
	local DodgeVelocity = Instance.new("LinearVelocity")
	DodgeVelocity.Attachment0 = attachment
	DodgeVelocity.MaxForce = 10000
	DodgeVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Vector
	DodgeVelocity.Parent = hrp

	-- For loop with steering
	local iterations = math.floor(DodgingTime / SteerInterval)
	local x = 0
	local a = 1
	local DeltaT = SteerInterval / DodgingTime
	for i = 1, iterations do
		x = x + DeltaT
		a = a - DeltaT
		local steer = MoveDir * a + LookVec * (1 - a)
		DodgeVelocity.VectorVelocity = steer * SpeedMult
		wait(SteerInterval)
	end

	-- Clean up
	Debris:AddItem(DodgeVelocity, 0)
	Debris:AddItem(attachment, 0)
	hm:SetAttribute("IsDodging", false)
	hm:SetAttribute("HasIFrames", false)
	task.delay(CooldownTime, function()
		plr:SetAttribute("CanDodge", true)
		print(plr.Name .. " can dodge again")
	end)
	print("Dodging")
end

(The server script is a snippet again but other than these parts are just setting up the attributes for the player character and some local variables)

Also, I have tried your solution, but it still doesn’t print anything.

I totally misread the first snippet and somehow thought it was inside a local script, oops.

The main issue, as far as I can tell, is here:

if IKCN == "Q" then
	DR:FireServer(IKCN, chr:WaitForChild("Humanoid").MoveDirection, chr:WaitForChild("HumanoidRootPart").CFrame.LookVector)
else
	IS:FireServer(IKCN)
end

Here you check if the Key they press is Q, then you fire the dodge Event. Otherwise you fire InputSender.

This means pressing the Q button wont fire the InputSender at all, so it will never Fire to the server with the keyName that it provides being “Q”.

On the server you listen for the InputSender, and only if the keyName that the client sends to it is Q, which in this code will never happen as I explained, do you then connect to the DodgeRemote on the Server, which btw, would start to fire multiple times with every time you press Q, since you connect to it everytime you press Q as well if it actually worked.

This means that the InputSender will never do anything except for print “Invalid Input” whenever you press any key other that is invalid. Meanwhile the Dodge Event will be fired when pressing Q, but never listened to on the server.

I see many problems with this code, but this should be the main reason that nothing works at all.


What my recommendations are for the other issues in your code:

I would recommend simply not using the Server for the Dodge Movement, instead do it on the client since the client has full server-side control over the character anyway.

What you should be doing on the server is that whenever the player dodges, you could fire to the server, check if the player can dodge there and simply set the attributes as required (which you are doing in the dodge function already). The dodging movement can and should be handled on the client.

EDIT: I am currently not sure anymore on how to do all this exactly, but I believe you have to handle the physics of the movement on the client, so that its smooth and responsive, but also the server at the same time, so it doesnt lag for the server or other clients, otherwise the physics of the dodge would only be handled on the client. Maybe look into some posts detailing this.

This way the server knows when the player is performing a dodge through the attributes, and this information can then be used when checking for suspicious movement in an Anti-Cheat. Which would probably check for how fast the player is moving and if that was actually be plausible or not.

So if they are at some point very fast, you would check (On the Server of course) if they are dodging, and if the speed they are dodging at is what it should be. If not, there are multiple things you could do, such as set them back to their last position, kick them, or even ban them.

For more detailed explanations of anti-cheats, you should probably look to other posts, as I dont do them often.


If you still want to use this method though, maybe try doing it without the DodgeRemote and simply do the following.

if IKCN == "Unknown" then
	return
else
	IS:FireServer(IKCN)
end
InputSender.OnServerEvent:Connect(function(plr, keyName)
	local action = ValidKeys[keyName]
	if action then
		print("Valid input received from: " .. plr.Name)
		if plr:GetAttribute("CanDodge") and keyName == "Q" then
            local character = plr.Character or plr.CharacterAdded:Wait() -- I saw you didnt do this in your dodge. This will try to get the player character and if it does not exist yet, it waits until it is added. 
            local Humanoid = character:WaitForChild("Humanoid")
            local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")

			Dodge(plr, Humanoid.MoveDirection, HumanoidRootPart.CFrame.LookVector)
		else
			print(plr.Name .. " is on dodge cooldown")
		end
	else
		print("Invalid input received from: " .. plr.Name)
	end
end)

Just in case, Im sorry if I made another mistake ;(

(I didnt look over the entire dodge function, but you said you checked it already, so I assume it works.)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.