You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
I’m making a knockback system for npcs in my game (my npcs are client-sided). Currently I’m trying to create a knockback that moves the NPC model rendered on the client to the position the server tells it to, but I’m not achieving accurate results. What i want to achieve is when the client has velocity applied to it, it goes straight to the server’s position (the red part)
- What is the issue? Include screenshots / videos if possible!
As you can see in this video the client model doesn’t go to it’s target (The red part on the server) and the npc also goes up which is a bit strange
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried things such as AssemblyLinearVelocity but that didn’t work out since the knockback came out way too fast for me. I have also tried changing the lifetime or the body velocity this might be a soultion i’ve tried to calculate the time it’d take to reach client model to reach the npc but i dont think that really works too
-- this is the knockback function(Server)
_G.ApplyKnockback = function(Player,folder,goal: Vector3?,speed,lifetime: number?)
local data = _G.characters[folder]
for i,Idata in pairs(data) do
data[i].cframe = goal
end
folder:SetAttribute("ApplyingKnockback",true)
--data[1].cframe = CFrame.new(goal) -- just set the position on the server and have client stimulate the actual knockback
for _,otherPlayer in pairs(game.Players:GetPlayers()) do
-- if otherPlayer == Player then continue end
local PlayerPos = Player.Character:WaitForChild("HumanoidRootPart").Position
local OtherPlayerPos = otherPlayer.Character:WaitForChild("HumanoidRootPart").Position
if (PlayerPos - OtherPlayerPos).Magnitude <= 500 then
ReplicateNPC:FireClient(otherPlayer,{
Model = "Rig",
Action = "StimulateKnockback",
goal = goal,
Speed = speed,
Lifetime = lifetime or 0.15,
})
end
end
task.delay(lifetime or 0.15,function()
folder:SetAttribute("ApplyingKnockback",nil)
end)
end
-- this is how im calling the function on the server
local phaseRange = (Skillsets[Style].M1S[Combo].MaxMag) or 7
local enemyPos = _G.characters[workspace.Characters.Rig][1].cframe
if (attackerPos - enemyPos).Magnitude <= phaseRange then
local enemyEntity
enemyPos = _G.characters[workspace.Characters.Rig][1].cframe
-- enemyEntity = CharacterManager.FindEntity(workspace.Characters.Rig.RigModelServer)
if Combo == 5 then
local forceApplied = enemyPos + hrp.CFrame.LookVector * 50
_G.ApplyKnockback(Player,workspace.Characters.Rig,forceApplied,50,0.833)
end
-- this is the client code that actual stimulates the knockback
local function applyKnockback(ModelRoot, targetPosition, speed)
local startPos = ModelRoot.Position
local distance = (targetPosition - startPos).Magnitude
local lifetime = distance / speed
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Parent = ModelRoot
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVelocity.Velocity = (targetPosition - startPos).Unit * speed
bodyVelocity.Name = "ClientBodyVelocity"
print("Applying BodyVelocity for lifetime:", lifetime)
task.delay(lifetime, function()
if bodyVelocity and bodyVelocity.Parent then
bodyVelocity:Destroy()
print("BodyVelocity destroyed at target time.")
end
end)
end
local Functions = {
["StimulateKnockback"] = function(Data)
local Model: Model? = _G.NPCs[Data.Model].Model
local Lifetime: number = Data.Lifetime
local TargetPos: Vector3 = Data.goal
local Speed: number = Data.Speed
local ModelRoot: Part? = Model:WaitForChild("HumanoidRootPart")
applyKnockback(ModelRoot, TargetPos, Speed)
end,
}
any help would be appreciated or if anyone could point out what im doing wrong