Help with ControlScript(i move even when im chatting)

so i have a controlscript(custom) and the thing is when i start chatting i starts moving and idk how to fix so pls help

local movements = {
	["forw"] = Enum.KeyCode.W,
	["left"] = Enum.KeyCode.A,
	["back"] = Enum.KeyCode.S,
	["righ"] = Enum.KeyCode.D,
}
local doJump = false
local ContextActionService = game:GetService("ContextActionService")
local inputs = setmetatable({}, {
	__index = function(t, k)
		return game:GetService("UserInputService"):IsKeyDown(movements[k]) and 1 or 0
	end
})
local uis = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local char = player.Character
player.CharacterAdded:Connect(function(newchar)
	char = newchar
end)
local humanoid
repeat wait()
	humanoid = char:WaitForChild("Humanoid")
until char:WaitForChild("Humanoid")
local function jump()
	if player.Character ~= nil then
		if player.Character.Humanoid.WalkSpeed ~= 0 then
			player.Character.Humanoid.Jump = true
		end
	end
end
local con = game:GetService("RunService").RenderStepped:Connect(function()
	if char and char:FindFirstChild("Humanoid") then
		if doJump == true then
			jump()
		end
		local movez = inputs.forw - inputs.back
		local movex = inputs.righ - inputs.left
		player.PlayerScripts.X.Value = movex
		player.PlayerScripts.Z.Value = movez
		player:Move(Vector3.new(0, 0, -movez) + Vector3.new(movex, 0, 0), true)		
	end
end)
ContextActionService:BindAction("Jump", function(action, userInputState, inputObject) doJump = (userInputState == Enum.UserInputState.Begin) end, false, Enum.KeyCode.Space, Enum.KeyCode.ButtonA)

here is the code

1 Like

Try to use Keybinds for WASD then

1 Like

what do you mean?
i dont understand

1 Like

I didn’t read the entire script but I can see that you are using UserInpupService for WASD, you can use Keybinds instead because they don’t work when chatting

Edit: I’m sorry if I’m wrong

1 Like

whats the difference between keybinds and userinputservice ??
i think they are the same

No, Keybinds aren’t working when you chat

1 Like

could you show an article in devhub or maybe a code a example of keybinds?

“It’s better to use ContextActionService’s BindAction than UserInputService.InputBegan for most cases. For InputBegan, your connected function would have to check if the player is in the context of the action begin performed. In most cases, this is harder than just calling a function when a context is entered/left. For example, if you want to have the H key trigger a car horn sound while the player is sitting in it, the player might type “hello” in chat or otherwise use the H key for something else. It is harder to determine if something else is using the H key (like chat) - the car might honk when the player didn’t mean to!”

1 Like

Just try to jump when you are chatting and it won’t work because you use Keybind

thx for the help but i made this and it worked:

local doJump = false
local ContextActionService = game:GetService("ContextActionService")
local uis = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local char = player.Character
local forw= 0
local back= 0
local r=0
local l = 0
uis.InputBegan:Connect(function(i,g)
	if i.KeyCode == Enum.KeyCode.W and not g then
		forw = 1
	end
	if i.KeyCode == Enum.KeyCode.A  and not g then
		l = 1
	end
	if i.KeyCode == Enum.KeyCode.S and not g then
		back = 1
	end
	if i.KeyCode == Enum.KeyCode.D and not g then
		r = 1
	end
end)
uis.InputEnded:Connect(function(i,g)
	if i.KeyCode == Enum.KeyCode.W and not g then
		forw = 0
	end
	if i.KeyCode == Enum.KeyCode.A  and not g then
		l = 0
	end
	if i.KeyCode == Enum.KeyCode.S and not g then
		back = 0
	end
	if i.KeyCode == Enum.KeyCode.D and not g then
		r = 0
	end
end)
player.CharacterAdded:Connect(function(newchar)
	char = newchar
end)
local humanoid
repeat wait()
	humanoid = char:WaitForChild("Humanoid")
until char:WaitForChild("Humanoid")
local function jump()
	if player.Character ~= nil then
		if player.Character.Humanoid.WalkSpeed ~= 0 then
			player.Character.Humanoid.Jump = true
		end
	end
end
local con = game:GetService("RunService").RenderStepped:Connect(function()
	if char and char:FindFirstChild("Humanoid") then
		if doJump == true then
			jump()
		end
		local movez = forw - back
		local movex = r - l
		player.PlayerScripts.X.Value = movex
		player.PlayerScripts.Z.Value = movez
		player:Move(Vector3.new(0, 0, -movez) + Vector3.new(movex, 0, 0), true)		
	end
end)
ContextActionService:BindAction("Jump", function(action, userInputState, inputObject) doJump = (userInputState == Enum.UserInputState.Begin) end, false, Enum.KeyCode.Space, Enum.KeyCode.ButtonA)

thx for the help again!

1 Like