hello! i am making thing: when player press any key - he start moving by itself in direction he’s looking at but after dying the system resets and to start moving again press any key. I tried making it with connections but after death and trying to connect connection again - humanoid:Move() stop working
script:
local plrs = game:GetService("Players")
local runService = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local plr = plrs.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local cam = workspace.CurrentCamera
local hum = char:WaitForChild("Humanoid")
local humRoot = char:WaitForChild("HumanoidRootPart")
local is_moving = false
local connection
local function startMovement()
is_moving = true
connection = runService.RenderStepped:Connect(function()
local cam_looking_vec = cam.CFrame.LookVector
local move_direction = Vector3.new(cam_looking_vec.X, 0, cam_looking_vec.Z).Unit
hum:Move(move_direction, false)
end)
end
hum.Died:Connect(function()
if connection then
connection:Disconnect()
connection = nil
is_moving = false
end
end)
uis.InputBegan:Connect(function(key)
if is_moving then return end
if key.KeyCode == Enum.KeyCode.W then
startMovement()
elseif key.KeyCode == Enum.KeyCode.S then
startMovement()
elseif key.KeyCode == Enum.KeyCode.A then
startMovement()
elseif key.KeyCode == Enum.KeyCode.D then
startMovement()
end
end)
uis.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.W then
hum.WalkSpeed = 25
elseif key.KeyCode == Enum.KeyCode.S then
hum.WalkSpeed = 10
end
end)
uis.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.W or key.KeyCode == Enum.KeyCode.S then
hum.WalkSpeed = 16
end
end)