Working on a realistic walking system for a horror game and I wanna create a slow walkspeed effect when walking backwards left or right

Hey, so if you didn’t read the title I want to create a slow walk speed system when walking backwards, left, or right. To go more in-depth when the player walks backwards set their movement speed to 4. When walking forward set their movement speed to 12. When walking left or right set it to 8, now this works fine and all but I want to improve it more so if you press two keys at the same time let’s say if you press W and A you’ll move backwards and left but your walk speed will be let’s say 10 because you’re pressing two movement keys how can I accomplish this without making 200 different combinations, and detect them when I release certain keys etc.

i’ve attempted it here:

--//Locals

local userInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character

local move_Forward = Enum.KeyCode.W
local move_Backward = Enum.KeyCode.S
local move_Right = Enum.KeyCode.D
local move_Left = Enum.KeyCode.A


--//Main

local walk_ = function(input, gameProcessed, gameProccessedEvent)
	
	--// Basic Combos \\--
	
	if userInputService:IsKeyDown(move_Forward) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 12
	end
	
	if userInputService:IsKeyDown(move_Backward) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 6
	end
	
	if userInputService:IsKeyDown(move_Right) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 6
	end
	
	if userInputService:IsKeyDown(move_Left) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 6
	end
	
	--// Advanced Combos
		
	--// W \\--
	
	if userInputService:IsKeyDown(move_Forward) and userInputService:IsKeyDown(move_Backward) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 0
	end
	
	if userInputService:IsKeyDown(move_Forward) and userInputService:IsKeyDown(move_Right) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 8
	end
	
	if userInputService:IsKeyDown(move_Forward) and userInputService:IsKeyDown(move_Left) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 8
	end
	
	--\\\\\\\\--
	
	
	--// S \\--
	
	if userInputService:IsKeyDown(move_Backward) and userInputService:IsKeyDown(move_Forward) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 0
	end

	if userInputService:IsKeyDown(move_Backward) and userInputService:IsKeyDown(move_Right) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 4
	end

	if userInputService:IsKeyDown(move_Backward) and userInputService:IsKeyDown(move_Left) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 4
	end
	
	--\\\\\\\\--
	
	--// D \\--
	
	if userInputService:IsKeyDown(move_Right) and userInputService:IsKeyDown(move_Forward) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 8
	end

	if userInputService:IsKeyDown(move_Right) and userInputService:IsKeyDown(move_Backward) and not gameProccessedEvent  then
		character.Humanoid.WalkSpeed = 4
	end

	if userInputService:IsKeyDown(move_Right) and userInputService:IsKeyDown(move_Left) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 0
	end
	
	--\\\\\\\\--
	
	--// A \\--

	if userInputService:IsKeyDown(move_Left) and userInputService:IsKeyDown(move_Forward) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 8
	end

	if userInputService:IsKeyDown(move_Left) and userInputService:IsKeyDown(move_Backward) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 4
	end

	if userInputService:IsKeyDown(move_Left) and userInputService:IsKeyDown(move_Right) and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 0
	end

	--\\\\\\\\--
	
end

local walk_End = function(input, gameProccessedEvent)
	
	if input.KeyCode == move_Forward and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 0
	end
	
	if input.KeyCode == move_Backward and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 0
	end
	
	if input.KeyCode == move_Right and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 0
	end
	
	if input.KeyCode == move_Left and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 0
	end
	
	--------
	
	if input.KeyCode == move_Forward and not gameProccessedEvent then
		character.Humanoid.WalkSpeed = 0
	end
	
end

userInputService.InputBegan:Connect(walk_)

userInputService.InputEnded:Connect(walk_End)
2 Likes

Context action service.

You can bind having it be slowed on one of the left/right keys via the context action service as you can bind multiple keycodes to one function.

1 Like

Alright so I went in to the default player settings module created by ROBLOX, it works the same as my code does up there except it has one problem and so does my code up there. When pressing for example ‘W’ and then ‘D’ it will set the walkspeed to 8 like normal except then if I release ‘D’ or ‘W’ the walkspeed wont change. And thats the problem.

1 Like

I had a similar situation as yours. Instead, I used mouse.KeyUp and mouse.KeyDown and detects wheater it is w,a,s, or d. if it is either of them, it puts it in a table variable, and the opposite for mouse.KeyUp (Remove from table) then you can read the table to find the certain things needed for the combos ect.

1 Like

could you maybe go in more depth? This is what I have accomplished so far can you explain the idea behind the inserting into the table and go more in depth about it?

local mouse = game.Players.LocalPlayer:GetMouse()

local keys = {}

mouse.KeyDown:Connect(function(input, gameProccessedEvent)
	
	if input == "w" then
		table.insert(keys, input)
		print(keys[1])
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 12
	end
	
	if input == "a" then
		table.insert(keys, input)
		print(keys[1])
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 8
	end
	
	if input == "s" then
		
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 4
	end
	
	if input == "d" then
		
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 8
	end
	
end)

mouse.KeyUp:Connect(function(input, gameProccessedEvent)

	if input == "w" then
		
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 12
	end

	if input == "a" then
		
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 8
	end

	if input == "s" then
		
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 4
	end

	if input == "d" then
		
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 8
	end

end)

I insert the input in the table then what and what do I do with it?

1 Like

No, don’t change the walkspeed right after the player pressed the button. the first 2, w, and a is correct.
here:

local mouse = game.Players.LocalPlayer:GetMouse()
local keys = {}
local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
	character = player.CharacterAdded:wait()
end

local function checkForCombos()
	--go through the advanced combos first
	if #keys >= 2 then
		if table.find(keys, "w") and table.find(keys, "s") then
			character.Humanoid.WalkSpeed = 0
		end
		if table.find(keys, "w") and table.find(keys, "a") then
			character.Humanoid.WalkSpeed = 8
		end
		if table.find(keys, "w") and table.find(keys, "d") then
			character.Humanoid.WalkSpeed = 8
		end
		if table.find(keys, "s") and table.find(keys, "a") then
			character.Humanoid.WalkSpeed = 4
		end
		if table.find(keys, "s") and table.find(keys, "d") then
			character.Humanoid.WalkSpeed = 8
		end
		if table.find(keys, "a") and table.find(keys, "d") then
			character.Humanoid.WalkSpeed = 0
		end
	elseif #keys == 1 then
		if table.find(keys, "w") then
			character.Humanoid.WalkSpeed = 12
		end
		if table.find(keys, "s") or table.find(keys, "a") or table.find(keys, "d") then
			character.Humanoid.WalkSpeed = 6
		end
	end
end

mouse.KeyDown:Connect(function(input)
	if input == "w" or input == "a" or input == "s" or input == "d" then
		table.insert(keys, input)
		checkForCombos()
	end
end)

mouse.KeyUp:Connect(function(input)
	if input == "w" or input == "a" or input == "s" or input == "d" then
		table.remove(keys, table.find(input))
		checkForCombos()
	end
end)

Edit: oops, there is a error, lemme fix rq

2 Likes

ok, just replace the end part where u remove the key form the table with this:

table.remove(keys, table.find(keys, input))

Also, in advanced combos, s and d should be walkspeed 4 not 8 oops

haha yeah already fixed both of those no problem It works great ill make sure to credit you if I release this.

No prob! (tell me the game name!)

It will be called TheRainCoatMan and I’m re-working all my mechanics for it to add a lot of realism to hopefully set the bar for all of roblox horror because now days the horror games aren’t looking so hot.

1 Like

I agree that these horror games these days arnt as good. Good Luck! bye!

i have a script for it do u want it

Hello, May I ask If I can use this script? I’m making horror game too, I will credit you if you want. Thanks :>