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)