How to apply a force

I would like to ask all the experts, how should I apply acceleration or force to the repelled player when one player attacks another and the other player has a repelling effect? How can I determine the direction of the repelled player

3 Likes
BasePart:ApplyImpulse()

Would apply an impulse aka a force to the player.

For direction do:

local vector1 = -- your vector 1
local vector2 = -- your vector 2
local Vector1ToVector2Direction = CFrame.new(vector1,vector2).LookVector
1 Like

I would probably use a linear velocity constraint. If you want to get the direction of the repelled player, you can get that vector like this:

local directionVector = (player2.Position - player.Position).Unit

Obviously you’ll need to adjust the code for getting the positions.

hey,buddy,Thank your reply,I apply the linearVelocity instead of force now.
But the attacked player didnt move, can you tell me the reason
Below is Code
localscript

Blockquotelocal Players = game:GetService(“Players”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local KnockBackEvent = ReplicatedStorage.Events:FindFirstChild(“KnockBackEvent”)
local player = Players.LocalPlayer
– 等待玩家角色以及部件载入
local character = player.Character or player.CharacterAdded:Wait()
local RightHand = character:WaitForChild(“RightHand”)
local humanoid = character:WaitForChild(“Humanoid”)

local debounce = true

local function onTouch(otherPart)
if debounce then
debounce = false
local character = otherPart.Parent
if character and character:FindFirstChild(“Humanoid”) then
local attackedPlayer = game:GetService(“Players”):GetPlayerFromCharacter(character)

		if attackedPlayer then
			if KnockBackEvent then
				--将被攻击的的玩家发送给服务器
				KnockBackEvent:FireServer(attackedPlayer)
			else
				warn("KnockBackEvent不存在")
			end
		end
		print("时间触发")
		wait(0.2)
		debounce = true
	end
end

end

RightHand.Touched:Connect(onTouch)
–处理事件
KnockBackEvent.OnClientEvent:Connect(function(direction)

local linearVelocity = Instance.new("LinearVelocity")
linearVelocity.Parent = player.Character.HumanoidRootPart
linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
linearVelocity.VectorVelocity = direction * 50

-- 等待3秒钟
wait(3)

-- 停止给予速度
linearVelocity.VectorVelocity = Vector3.new(0, 0, 0)

end)

the Script in server

Blockquote
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local QueueEvent = ReplicatedStorage.Events:FindFirstChild(“QueueEvent”)
local CountDownEvent = ReplicatedStorage.Events:FindFirstChild(“COUNTDOWN”)
local TouchEvent = ReplicatedStorage.Events:FindFirstChild(“TouchEvent”)
local KnockBackEvent = ReplicatedStorage.Events:FindFirstChild(“KnockBackEvent”)
–处理玩家击退事件
KnockBackEvent.OnServerEvent:Connect(function(player,attackedPlayer)
– 计算中心点坐标
local center = (player.Character.HumanoidRootPart.Position + attackedPlayer.Character.HumanoidRootPart.Position) / 2

-- 计算从中心点到目标玩家的向量
local direction = (attackedPlayer.Character.HumanoidRootPart.Position - center).Unit
direction = Vector3.new(direction.X, 0, direction.Z) -- 限制在XOZ平面上
KnockBackEvent:FireClient(attackedPlayer,direction)

end)

I won’t help you till you properly format this code. Use “```” before and after your code block to format it.

I’m really sorry, I didn’t know how to use this before. Thank you very much for your suggestion. Here is the whole code.

-- This is an example Lua code block
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KnockBackEvent = ReplicatedStorage.Events:FindFirstChild("KnockBackEvent")
local player = Players.LocalPlayer
-- 等待玩家角色以及部件载入
local character = player.Character or player.CharacterAdded:Wait()
local RightHand = character:WaitForChild("RightHand")
local humanoid = character:WaitForChild("Humanoid")

local debounce = true

local function onTouch(otherPart)
	if debounce then
		debounce = false
		local character = otherPart.Parent
		if character and character:FindFirstChild("Humanoid") then
			local attackedPlayer = game:GetService("Players"):GetPlayerFromCharacter(character)

			if attackedPlayer then
				if KnockBackEvent then
					--将被攻击的的玩家发送给服务器
					KnockBackEvent:FireServer(attackedPlayer)
				else
					warn("KnockBackEvent不存在")
				end
			end
			print("时间触发")
			wait(0.2)
			debounce = true
		end
	end
end

RightHand.Touched:Connect(onTouch)
--处理事件
KnockBackEvent.OnClientEvent:Connect(function(direction)
	
	local linearVelocity = Instance.new("LinearVelocity")
	linearVelocity.Parent = player.Character.HumanoidRootPart
	linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
	linearVelocity.VectorVelocity = direction * 50

	-- 等待3秒钟
	wait(3)

	-- 停止给予速度
	linearVelocity.VectorVelocity = Vector3.new(0, 0, 0)
end)
-- this is a script in ServerScriptService
--处理玩家击退事件
KnockBackEvent.OnServerEvent:Connect(function(player,attackedPlayer)
	-- 计算中心点坐标
	local center = (player.Character.HumanoidRootPart.Position + attackedPlayer.Character.HumanoidRootPart.Position) / 2
	
	-- 计算从中心点到目标玩家的向量
	local direction = (attackedPlayer.Character.HumanoidRootPart.Position - center).Unit
	direction = Vector3.new(direction.X, 0, direction.Z) -- 限制在XOZ平面上
	KnockBackEvent:FireClient(attackedPlayer,direction)
end)

Hey, buddy ,
Do you know what the problem is with these codes

LinearVelocity requires an Attachment0 (and Attachment1 depending on the mode). And I’m uncertain if LinearVelocities are Enabled as default (they probably are).

image

OK ,thanks very much.I ll put
An attachment to retry

Remove this entire part. 
KnockBackEvent.OnServerEvent:Connect(function(player,attackedPlayer)
	-- 计算中心点坐标
	local center = (player.Character.HumanoidRootPart.Position + attackedPlayer.Character.HumanoidRootPart.Position) / 2
	
	-- 计算从中心点到目标玩家的向量
	local direction = (attackedPlayer.Character.HumanoidRootPart.Position - center).Unit
	direction = Vector3.new(direction.X, 0, direction.Z) -- 限制在XOZ平面上

	local linearVelocity = Instance.new("LinearVelocity")
	local attachment = Instance.new("Attachment")
	attachment.Parent = player.Character.HumanoidRootPart
	linearVelocity.Attachment0 = attachment -- you forgot the attachment for this.
	linearVelocity.Parent = player.Character.HumanoidRootPart
	linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
	linearVelocity.VectorVelocity = direction * 50
	game:GetService("Debris"):AddItem(linearVelocity,1) -- Destroys the velocity after 1 second. It is not a good practice to have the velocity exists because it can stop all movement of the player.
end)

I combined a few things and this should potentially work now.

I’d also do is combine the 2 codes because there is no need to have 2 scripts here:

-- This is an example Lua code block
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KnockBackEvent = ReplicatedStorage.Events:FindFirstChild("KnockBackEvent")
local player = Players.LocalPlayer
-- 等待玩家角色以及部件载入
local character = player.Character or player.CharacterAdded:Wait()
local RightHand = character:WaitForChild("Right Hand")
local humanoid = character:WaitForChild("Humanoid")

local debounce = true

local function onTouch(otherPart)
	if debounce then
		debounce = false
		local othercharacter = otherPart.Parent
		if othercharacter and othercharacter:FindFirstChild("Humanoid") then
			local attackedPlayer = game:GetService("Players"):GetPlayerFromCharacter(othercharacter)

			if attackedPlayer then
				
				local direction = (othercharacter.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Unit
				
				local linearVelocity = Instance.new("LinearVelocity")
				local attachment = Instance.new("Attachment")
				attachment.Parent = player.Character.HumanoidRootPart
				linearVelocity.Attachment0 = attachment -- you forgot the attachment for this.
				linearVelocity.Parent = player.Character.HumanoidRootPart
				linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
				linearVelocity.VectorVelocity = direction * 50
				game:GetService("Debris"):AddItem(linearVelocity,1)
			end
			print("时间触发")
			wait(0.2)
			debounce = true
		end
	end
end

RightHand.Touched:Connect(onTouch)
1 Like

OK,thanks very much,I ll try the corrected
code after my class

1 Like