Velocity Not Working [SOLVED]

server script code:

local remote = game.ReplicatedStorage:WaitForChild("Hitted1")

remote.OnServerEvent:Connect(function(target , mouseCFrame)
	if target then
		if target.Parent:FindFirstChild("Humanoid") then
			local lookVector = mouseCFrame.LookVector --Gets mouse CFrame

			if mouseCFrame.LookVector.Y < 0 then 
				lookVector = mouseCFrame.LookVector * Vector3.new(1,-1,1) --This is only to make player knockback upwards, and not downwards. You can remove this if you want.
			end

			local root = target.Parent.HumanoidRootPart
			local velocity = Instance.new("BodyVelocity")

			print("Hit"..target.Name)
			velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			velocity.P = 1500
			velocity.Velocity = (target.HumanoidRootPart.CFrame.LookVector * -1) * 100
			velocity.Parent = root
			wait(0.5)
			velocity:Destroy()
--- server script ^
		end
	end
end)

local script code:

local tool = script.Parent

local remote = game.ReplicatedStorage:WaitForChild("Hitted1")

local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()

tool.Activated:Connect(function()
	remote:FireServer(mouse.Target, true, "ErrorPart Equipped")
end)
--- local script ^

script above is not working
it’s supposed to knockback the target player
no errors
any help will work and post with the script

1 Like

bro i dont think you have to go that far

you can just do

(target.HumanoidRootPart.CFrame.LookVector * -100)

It doesn’t work

It does nothing

No errors right? Then let’s relook at what you’re trying to do here. Basically, you are detecting inputs from the user and use a RemoteEvent to add BodyVelocity to the target if it is a valid character with a Humanoid.

Alright, if you’re not testing it with someone, then it means you’re testing it with a NPC. Have you checked if the NPC is anchored? If the NPC is anchored, force such as BodyVelocity will not work on the NPC. (when i spawn npc from roblox studio, the humanoidrootpart is usually anchored, so check that)

I’m testing it with a player not npc

if i test it with a npc the BodyVelocity doesn’t work even if i unanchored the HumanoidRootPart

When any humanoid isnt sitting any velocidy aplied are nulified, you may want set Sit of humanoid to true before aplying velocity

how would i do that?

need more characters

character.Humanoid.Sit = true

Also I just noticed

First argument for OnServerEvent event is a player that triggered remote event and then arguments that you specified in localscript.

it just displays a warning message:

Change

remote.OnServerEvent:Connect(function(target , mouseCFrame)

to

remote.OnServerEvent:Connect(function(plr, target , mouseCFrame)

plr is the player who fired the remote.

it will just break the code

.

What do you mean? Your code won’t work either way because otherwise it assigns the Player argument to your target parameter.

tried that already (doesn’t work)

I think it’s also because you’re passing over the following datatypes when firing the remote:
:FireServer(BasePart?, boolean, string)
When the server accepts
:Connect(function(BasePart?, CFrame)
Basically, you should instead fire the remote with
:FireServer(mouse.Target, mouse.Hit)

Still doesn’t work

.

--LOCAL

local replicated = game:GetService("ReplicatedStorage")
local remote = replicated:WaitForChild("Hitted1")

local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

local tool = script.Parent

tool.Activated:Connect(function()
	if mouse.Target then
		remote:FireServer(mouse.Target, mouse.Hit)
	end
end)
--SERVER

local replicated = game:GetService("ReplicatedStorage")
local remote = replicated.Hitted1

remote.OnServerEvent:Connect(function(player, mouseTarget, mouseHit)
	local targetModel = mouseTarget:FindFirstAncestorOfClass("Model")
	if targetModel then
		local targetHuman = targetModel:FindFirstChildOfClass("Humanoid")
		if targetHuman then
			local targetRoot = targetModel:FindFirstChild("HumanoidRootPart")
			if targetRoot then
				local bodyVelocity = Instance.new("BodyVelocity")
				bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
				bodyVelocity.P = 1500
				bodyVelocity.Velocity = (-targetRoot.CFrame.UpVector) * 100
				bodyVelocity.Parent = targetRoot
				task.wait(0.5)
				bodyVelocity:Destroy()
			end
		end
	end
end)

This is working for me (assuming you wanted to create a fling tool).

image

file.rbxm (4.4 KB)

1 Like

it works but sometimes it pushes the player down the platform

(-targetRoot.CFrame.UpVector) * 100

Replace this bit with.

(targetRoot.CFrame.UpVector) * 100

I wasn’t sure if you wanted to fling down or up.