How to fix this Issue?

Hello, I have this problem: when I punch while moving, my character teleports on other players’ screen.

Look at the player screen on the left in the video.

-Video Link -Watch 2024-03-23 19-11-17 | Streamable

local RS = game:GetService("ReplicatedStorage")

local RCombat = RS.Remotes.Combat

local Status = require(RS.Modules.CharacterLoader)
local Target = require(script.TargetHandler)


RCombat.OnServerEvent:Connect(function(Player, Combo, Result)
	
	task.spawn(function()
		Target.Handler(Player, Combo, Result) -- this where I do my damage, knockback, etc.
	end)

	RCombat:FireAllClients(Result, Combo) -- I do all animations and effects here
	
end)
1 Like

pls give us more details of your codes but :FireAllClients() is kinda unreliable

1 Like

Hard to tell without more information (odds are that you’re not accepting position updates/not sending them/locking the player in some way (welds?)), but as a side note, task.spawn() accepts overload variables so you could simplify the lines

task.spawn(function()
	Target.Handler(Player, Combo, Result) -- this where I do my damage, knockback, etc.
end)

into:

task.spawn(Target.Handler, Player, Combo, Result) -- this where I do my damage, knockback, etc.

View the page for task.spawn

1 Like

I’m unsure if my script is efficient or reliable; I’ve only coded it based on what I’ve learned in the past few weeks since I started scripting. I would appreciate it if you could also offer me some tips on how to improve my script. :smiling_face:

Here’s the copy of my game.
Test.rbxl (82.6 KB)

Client Script-

local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Debris = game:GetService("Debris")

local RCombat = RS.Remotes.Combat

local Animation = require(script.Animations)
local Hitbox = require(script.Hitbox)

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Hum = Character:FindFirstChild("Humanoid")
local Hrp = Character:FindFirstChild("HumanoidRootPart")

local Combo = 1
local Cooldown = 0
local LastCooldown = 0

UIS.InputBegan:Connect(function(Input, GPE)
	if GPE then return end
	if Character:GetAttribute("Stunned", true) then return end
	
	if Input.UserInputType == Enum.UserInputType.MouseButton1 and tick() - Cooldown > .4 and tick() - LastCooldown > .8 then
		if tick() - Cooldown > 1.5 then Combo = 1 end
		
		Cooldown = tick()
		print(Combo)
		
		Animation.Combat(Combo)
		
		local Result = Hitbox.Create("Visual", Hrp.CFrame*CFrame.new(0,0,-2), Vector3.new(5,6,3))
		
		
		RCombat:FireServer(Combo, Result)
		
		
		if Combo == 4 then
			Combo = 1
			LastCooldown = tick()
		else
			Combo += 1
		end
		
		Hum.WalkSpeed = 7

		local OldCombo = Combo
		task.delay(.5, function()
			if Combo == OldCombo then
				Hum.WalkSpeed = 16
			end
		end)
	end

	if Input.KeyCode == Enum.KeyCode.Q and tick() - Cooldown > 1 then
		
		Cooldown = tick()
		
		local function Dash(Distance, Increment)
			
			local DashVelocity = Instance.new("LinearVelocity", Hrp)
			DashVelocity.Attachment0 = Hrp.RootAttachment
			DashVelocity.ForceLimitMode = Enum.ForceLimitMode.PerAxis
			DashVelocity.MaxAxesForce = Vector3.new(10000,0,10000)
				
			Animation.Dash("FrontDash")
			
			for i = Distance, 0, Increment do
				
				local Result = Hitbox.Create("Visual", Hrp.CFrame*CFrame.new(0,0,-2), Vector3.new(5,6,7))
				
				DashVelocity.VectorVelocity = Hrp.CFrame.LookVector * i
				task.wait()
				
				if #Result ~= 0 then
					
					Animation.Dash("DashPunch")
					RCombat:FireServer("Dashing", Result)
					i = 0
					
				end
				
				if i == 0 then
					DashVelocity:Destroy()
					Hum.WalkSpeed = 16
					break
				end
			
			end
			
		end
		
		if UIS:IsKeyDown(Enum.KeyCode.W) then
			Hum.WalkSpeed = 0
			
			Dash(70, -1)
			
		end
		
	end
	
end)

UIS.JumpRequest:Connect(function()
	
	if tick() - Cooldown < .3 then
		Hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
	else
		Hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
	end
	
end)

RCombat.OnClientEvent:Connect(function(Result, Combos)
	
	if Combos == "Dashing" then return end
	
	local SFX = RS.SFX.Punch

	local PunchSFX = {
		
		SFX["PunchSwing(1)"]:Clone(),
		SFX["Punch Swing(2)"]:Clone(),
		SFX["Light Punch(1)"]:Clone(),
		SFX["Light Punch(2)"]:Clone(),
		SFX["Hard Punch"]:Clone(),
		
	}
	
	
	
	if #Result == 0 then
		for i = 0,.5,0.1 do
			PunchSFX[1].Volume = i
			PunchSFX[2].Volume = i
			
		end

		if Combo <= 2 then
			PunchSFX[1].Parent = Hrp
			PunchSFX[1]:Play()
			Debris:AddItem(PunchSFX[1], .5)
			
		else
			PunchSFX[2].Parent = Hrp
			PunchSFX[2]:Play()
			Debris:AddItem(PunchSFX[2], .5)
			
		end
		
		
	elseif #Result ~= 0 then
		
		if Combos <= 2 then
			task.wait(.2)
			PunchSFX[3].Parent = Hrp
			PunchSFX[3]:Play()
			Debris:AddItem(PunchSFX[3], .8)

		elseif Combos == 3 then
			task.wait(.2)
			PunchSFX[4].Parent = Hrp
			PunchSFX[4]:Play()
			Debris:AddItem(PunchSFX[4], .8)
			
		else
			task.wait(.3)
			PunchSFX[5].Parent = Hrp
			PunchSFX[5]:Play()
			Debris:AddItem(PunchSFX[5], .8)

		end
		
	end
	
	
	
	for _, v in pairs (Result) do
		
		local HitEffect = RS.VFX["HitFx "]:Clone()
		HitEffect.Parent = v.HumanoidRootPart
		HitEffect.CFrame = v.HumanoidRootPart.CFrame
		
		for _, effect in pairs (HitEffect:GetChildren()) do
			
			if effect:IsA("ParticleEmitter") then
					effect:Emit(effect:GetAttribute("EmitCount"))
				
			end
			
		end
		
	end
	
end)

TargetHandler Module-

local TargetHandler = {}

local Time = 0
local TimerRunning = false

TargetHandler.Handler = function(Player, Combo, Result)

	local Character = Player.Character or Player.CharacterAdded:Wait()

	local function Timer(v)

		Time += 0.1
		
		if TimerRunning == false then

			TimerRunning = true

			repeat 
				task.wait(.8)
				Time -= 0.1
				print(Time)
			until Time <= 0

		end
	end


	for _, v in pairs (Result) do

		v.Humanoid:TakeDamage(3)
		
		task.spawn(function()
			
			v:SetAttribute("Stunned", true)
			v.Humanoid.WalkSpeed = 0
			v.Humanoid.JumpHeight = 0
			Timer(v)

			if Time <= 0 then
				TimerRunning = false
				v:SetAttribute("Stunned", false)
				v.Humanoid.WalkSpeed = 16
				v.Humanoid.JumpHeight = 7
			end
			
		end)

		if Combo == 4 then
			
			task.wait(.3)
			
			v:WaitForChild("RagdollTrigger").Value = true
			
			local Knockback = Instance.new("LinearVelocity", v.HumanoidRootPart)
			Knockback.Attachment0 = v.HumanoidRootPart.RootAttachment
			Knockback.ForceLimitMode = Enum.ForceLimitMode.PerAxis
			Knockback.MaxAxesForce = Vector3.new(math.huge, 0, math.huge)
			
			
			for i = math.random(50,60),0,-1 do
				
				Knockback.VectorVelocity = Character.HumanoidRootPart.CFrame.LookVector * i
				task.wait()
				
				if i == 0 then
					print("Knockback Stopped")
					Knockback:Destroy()
					
					task.delay(4, function()
						
						v:WaitForChild("RagdollTrigger").Value = false
						
					end)
				end
				
			end
			
		else
			
			local EnemyKnockback = Instance.new("LinearVelocity", v.HumanoidRootPart)
			EnemyKnockback.Attachment0 = v.HumanoidRootPart.RootAttachment
			EnemyKnockback.MaxForce = math.huge
			
			local Knockback = Instance.new("LinearVelocity", Character.HumanoidRootPart)
			Knockback.Attachment0 = Character.HumanoidRootPart.RootAttachment
			Knockback.MaxForce = math.huge
			
			for i = 10,0,-1 do

				EnemyKnockback.VectorVelocity = Character.HumanoidRootPart.CFrame.LookVector * i
				Knockback.VectorVelocity = Character.HumanoidRootPart.CFrame.LookVector * i
				task.wait()
				
				if i == 0 then
					Knockback:Destroy()
					EnemyKnockback:Destroy()
					
				end

			end
		end



	end
	
end

return TargetHandler

Okay so I think it’s because you’re applying knockback on the server (Lines 80-100 in ServerScriptService>CombatHandler>TargetHandler) and that doesn’t really work out if characters are client-owned, the simplest solution is asking the clients to handle their own knockback, which isn’t very elegant, but server-sided knockback is a whole 'nother can of worms that you’re welcome to look into if you’d like

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.