Problems with sword "Step Forward Script" (Using BodyVelocity)

I’ve made this script that allows the player to be moved a few studs via BodyVelocity when they use/swing a sword. I’ve ran into some problems with the code I can’t seem to find a solution for.

These problems are :

  1. Sword “slash” animations and the “Step Forward” script are in time with each other in the first few activations of the sword, but eventually drifts out of time (Though this could be just problems with timing in the script)

  2. If the sword is activated again at a certain time (Just as the “Step forward script” has finished) it will swing the sword as normal, but will completely ignore this “Step forward” script and allow the player move around freely instead of being moved by the BodyVelocity.

(The Sword is made from two scripts, this bit of code is in the Client side script)

(At this moment in time I cannot post any videos, though I’ve tried to explain the problems the best I can)

--Step Forward
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")

--Settings
local Distance = 15
local lastClick = tick()
local Debounce = true

local tool = script.Parent

tool.Activated:Connect(function()
	if Debounce then
		Debounce = false
		if tick() -lastClick > 0.51 then 
			local bodyVelocity = Instance.new("BodyVelocity", root)
			bodyVelocity.Velocity = root.CFrame.LookVector*Distance
			bodyVelocity.MaxForce = Vector3.new(1, 1, 1)* 500000
			wait(0.3)
			bodyVelocity:Destroy()
			lastClick = tick()
		end
		if not Debounce then
			character.Humanoid.WalkSpeed = 0.1 --Basically Stood Still but allows player to rotate
			character.Humanoid.JumpHeight = 0 --Stops player from jumping while stepping forward
			wait(0.5)
			character.Humanoid.WalkSpeed = 16
			character.Humanoid.JumpHeight = 7.2
		end
		Debounce = true
	end
end)


1 Like
--Step Forward
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local root = character:WaitForChild("HumanoidRootPart")

--Settings
local distance = 15
local lastClick = tick()
local debounce = true
local tool = script.Parent

tool.Activated:Connect(function()
	if debounce then
		return
	end
	debounce = true
	
	task.spawn(function()
		if tick() - lastClick > 0.5 then 
			local bodyVelocity = Instance.new("BodyVelocity")
			bodyVelocity.Parent = root
			bodyVelocity.Velocity = root.CFrame.LookVector * distance
			bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			task.wait(0.5)
			bodyVelocity:Destroy()
			lastClick = tick()
		end
	end)
	
	task.spawn(function()
		humanoid.WalkSpeed = 0
		humanoid.JumpHeight = 0
		humanoid.JumpPower = 0
		task.wait(0.5)
		humanoid.WalkSpeed = 16
		humanoid.JumpHeight = 7.2
		humanoid.JumpPower = 50
	end)
	
	task.wait(0.5)
	debounce = false
end)

Can you share the other script too please?

The other script is just the sword manager (Damage, Swing Combo Etc) which I know doesn’t conflict with this script, As this script can be used by it’s self and still have the listed bugs (Though you won’t see animations)

Also assuming the script you’ve put here is intended to rely on the Server Side script, because it doesn’t work by itself (Excluding all the code that allows the animations to play)

Script looks fine to me, a problem I would like to point out is that relying on timer functions for accurate syncing is bad practice, using something like signals would be much better as timer functions can be inaccurate (this may be the cause of your first problem), Your second problem I can’t really diagnose without the second script

Ok I’ll look into signals see if it would help, and since more that one person has asked for the other script I’ll put it up to help for diagnosing the problem


local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local animRemote = tool:WaitForChild("AnimationRemote")

local canDamage = false
local Debounce = true

local comboCount = 1
local lastSwing = tick()
local Deb = {}

local PushForce = 50

local function Push(TargetRoot)
	if TargetRoot then
		
		local TowardTarget = CFrame.new(MyRoot.Position, TargetRoot.Position).LookVector
		TargetRoot.Velocity = TowardTarget * PushForce
		
	end
end

handle.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	local HumanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")
	if humanoid and not Deb[humanoid] and canDamage then
		Deb[humanoid] = true
		humanoid:TakeDamage(50)
		if MyRoot then
			Push(HumanoidRootPart)
		end
	end
end)

tool.Activated:Connect(function()
	if Debounce then
		Debounce = false
		canDamage = true
		Deb = {}
		-- Combo System
		if tick() -lastSwing > 1.5 or comboCount >= 4 then
			comboCount = 1
			
		else
			comboCount = comboCount + 1
			-- now combo stage 2 
		end
		
		animRemote:FireAllClients(comboCount) --Tells client to play swing anim 
		lastSwing = tick()
		wait(0.7)
		canDamage = false
		Debounce = true
		
		
	end
end)

tool.Equipped:Connect(function()
	MyRoot = tool.Parent:FindFirstChild("HumanoidRootPart")
end)


If needed I can put up the Client Side though it is just handling animations

I’d imagine the main issue is simply relying on that the 2 scripts execute correctly together in sync, code in lua cannot run in parallel(without actors) I would suggest just combining the 2 scripts and maybe the usage of timer functions may be bad here too

I’ve tried combining the scripts but since the “Server Side” script will only work as a normal script it will not work with the “Step Forward/Client Side” script (Which I forgot to mention was a localscript)

Also when you mean signals do you mean Events or am I looking at the wrong thing?

So trying to sync 2 scripts is pretty questionable, syncing 2 scripts that are from different machine is very questionable, and by signals I do mean events

I agree, I don’t even know why I tried that.
BUT
After I turned the “Step Forward” into an event I was able to perfectly sync the animation with the “Step Forward” (After a few tests). The only problem is now if the player is holding down WSA or D sometimes it ignores the event. (Bug No.2 has kind of now been replaced with this bug)