Movement system needing fixes

I am writing a custom movement system for my game. However, I want to make it compatible with holding the movement key, plus my script is inefficient and sometimes registers the player as dead even if they are not.

All help is greatly appreciated!

Script:

local jerry = workspace.Jerry
local debounce = false
local stepLength = 0.25
local deathLength = 0.5
local respawnTime = 1
local hangTime = 0.5

jerry.Position = Vector3.new(0,1,0)

local uis = game:GetService("UserInputService")

local tweenService = game:GetService('TweenService')

local partPosTable = {}

function GetPartsAtPosition(Position)
	if table.find(partPosTable,Position) then
		return true
	else
		return false
	end
end

function generatePartPosTable(list)
	for _,obj in pairs(list) do
		table.insert(partPosTable,obj.Position)
	end
	print(partPosTable)
end

function regenerateJerry()
	local jerryExists =  workspace:FindFirstChild("Jerry")
	if jerryExists then
		jerryExists.Color = Color3.fromRGB(202, 203, 209)
		jerryExists.Position = Vector3.new(0,1,0)
	else
		error("Jerry was not found :(")
	end
end

generatePartPosTable(workspace.Land:GetChildren())

uis.InputBegan:Connect(function(input)
	if debounce == false then
		debounce = true
		local destination = false
		if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.Up then
			destination = Vector3.new(jerry.Position.X,jerry.Position.Y,jerry.Position.Z-1)
		elseif input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.Down then
			destination = Vector3.new(jerry.Position.X,jerry.Position.Y,jerry.Position.Z+1)
		elseif input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.Left then
			destination = Vector3.new(jerry.Position.X-1,jerry.Position.Y,jerry.Position.Z)
		elseif input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.Right then
			destination = Vector3.new(jerry.Position.X+1,jerry.Position.Y,jerry.Position.Z)
		end
		if destination then
			tweenService:Create(
				jerry,TweenInfo.new(stepLength,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut),
			{Position = destination}):Play()
			task.wait(stepLength)
			local belowParts = GetPartsAtPosition(Vector3.new(destination.X,destination.Y-0.75,destination.Z))
			if belowParts then
				print("Jerry is on land!")
				debounce = false
			else
				print("Jerry is dead.")
				task.wait(hangTime)
				tweenService:Create(
					jerry,TweenInfo.new(deathLength,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut),
					{Position = Vector3.new(jerry.Position.X,-5,jerry.Position.Z)}):Play()
				tweenService:Create(
					jerry,TweenInfo.new(deathLength,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut),
					{Color = Color3.fromRGB(209, 95, 95)}):Play()
				task.wait(deathLength+respawnTime)
				regenerateJerry()
				debounce = false
			end
		end
		debounce = false
	end
end)
1 Like

Update: I fixed the random dead blocks that would kill the player, but it still feels incredibly inefficient and holding down keys still does not work. All help is appreciated! never mind lol

use a holding feature by also tracking input ended
so like
make a variable that tracks whether a button is being holded down
and make a runservice renderstepped function that updates the character accordingly

1 Like

You should do the input check in a loop.
Either have variables / a table for keys pressed, listen to InputBegan and InputEnded and change the variables accordingly. Then in your loop check if the keys are pressed and do your movement logic.

You could also use UserInputService:GetKeysPressed() in the loop, however it returns keys that are processed (it returns keys that are pressed even if you’re typing in a textbox) so you’ll need to do some extra stuff to make sure they aren’t pressed

1 Like