Hello! I have made a punch function for my game that, for now, only deals knockback.
However, it seems like the Server is not receiving the events from the RemoteEvent.
It is probably a mistake in the LocalScript, as that sends the Event to server.
Local:
local Players = game:GetService("Players")
local LPlr = Players.LocalPlayer
local ImputS = game:GetService("UserInputService")
local RepStore = game:GetService("ReplicatedStorage")
local kEvent = RepStore.Events.KnockbackM
local Mouse = LPlr:GetMouse()
local Tool = script.Parent
local AnimationID = 8388173238
Tool.Activated:Connect(function()
local Tar = Mouse.Target
local char = LPlr.Character
local human = char:FindFirstChildWhichIsA("Humanoid")
local animator = human:FindFirstChildWhichIsA("Animator")
animator:LoadAnimation(AnimationID)
kEvent:FireServer(Tar)
end)
Server:
local Event = script.Parent.KnockbackM
local Speed = 20
local MaxForce = 80000
Event.OnServerEvent:Connect(function(player,target)
print("Fired")
local Vel = Instance.new("BodyVelocity")
Vel.Parent = target
Vel.MaxForce = MaxForce
Vel.P = 10
Vel.Velocity = target.CFrame.LookVector*target
end)
There are a couple of nitpicks, but I suppose I’ll focus on the main issue here:
You’re attempting to set a Vector3 value as a Number value, which results in the main error
And your Velocity property is trying to multiply a Vector3 value with a Instance, which can’t work
Instead, use the Character who fired the RemoteEvent & multiply it by their LookVector instead:
local Event = script.Parent.KnockbackM
local MaxForce = Vector3.new(80000, 80000, 80000)
local Speed = 20
Event.OnServerEvent:Connect(function(Plr, Target)
print("Fired")
local Char = Plr.Character
if Char and Char:FindFirstChild("HumanoidRootPart") then
local Vel = Instance.new("BodyVelocity")
Vel.MaxForce = MaxForce
Vel.P = 10
Vel.Velocity = Char.HumanoidRootPart.LookVector * Speed
end
end)
On your LocalScript I’d recommend checking for a valid Target first before sending a request to your RemoteEvent cause there is the possibility of Mouse.Target returning back nil
local Players = game:GetService("Players")
local LPlr = Players.LocalPlayer
local char = LPlr.Character or LPlr.CharacterAdded:Wait()
local Animator = char:WaitForChild("Humanoid"):WaitForChild("Animator")
local ImputS = game:GetService("UserInputService")
local RepStore = game:GetService("ReplicatedStorage")
local kEvent = RepStore.Events.KnockbackM
local Mouse = LPlr:GetMouse()
local Tool = script.Parent
local AnimationID = 8388173238
Tool.Activated:Connect(function()
local Tar = Mouse.Target --Target refers to the Object that the mouse is currently on
if Tar and Tar.Parent and Tar.Parent:FindFirstChild("Humanoid") then
Animator:LoadAnimation(AnimationID)
kEvent:FireServer(Tar)
end
end)
Nah, if you’re using a RemoteEvent that shouldn’t be the case cause it should detect when it’s supposed to Fire
I’d check for any Output errors in your console if you can please, cause it’s kinda difficult to determine what issue you’re exactly dealing with
Is there anything at least printing then…? I’d check for a print on the client side as well to make sure that the LocalScript is fully running & detecting when it’s supposed to fire
From what I’m assuming then, it looks like something within your LocalScript doesn’t seem to be working correctly then
Can you try this, and see what outputs back?
print("We know this works")
local Players = game:GetService("Players")
local LPlr = Players.LocalPlayer
local char = LPlr.Character or LPlr.CharacterAdded:Wait()
local Animator = char:WaitForChild("Humanoid"):WaitForChild("Animator")
local ImputS = game:GetService("UserInputService")
local RepStore = game:GetService("ReplicatedStorage")
local kEvent = RepStore.Events.KnockbackM
local Mouse = LPlr:GetMouse()
local Tool = script.Parent
local AnimationID = 8388173238
print("Loaded everything")
Tool.Activated:Connect(function()
local Tar = Mouse.Target --Target refers to the Object that the mouse is currently on
print(Tar)
if Tar and Tar.Parent and Tar.Parent:FindFirstChild("Humanoid") then
print(Tar.Parent)
Animator:LoadAnimation(AnimationID):Play()
kEvent:FireServer(Tar)
end
end)
For the player receiving knockback it must be ran in their client. The client controls its character’s physics and not the server. So applying velocity to a player’s character from server will do nothing because the server does not own it.
An Alternative is setting the Humanoid receiving knockback state to Physics:
I think this should give server ownership of the character. I am not sure about this tho.
I tried this but it doesn’t work. The Animation plays but no knockback is given.
Invoker Local
local Players = game:GetService("Players")
local LPlr = Players.LocalPlayer
local char = LPlr.Character or LPlr.CharacterAdded:Wait()
local Animator = char:WaitForChild("Humanoid"):FindFirstChildWhichIsA("Animator")
local ImputS = game:GetService("UserInputService")
local RepStore = game:GetService("ReplicatedStorage")
local kEvent = RepStore.Events.KnockbackM
local Mouse = LPlr:GetMouse()
local Tool = script.Parent
local AnimationID = "rbxassetid://8388173238"
function SendReq()
local Tar = Mouse.Target --Target refers to the Object that the mouse is currently on
if Tar and Tar.Parent:FindFirstChild("Humanoid") then
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationID
local punch = Animator:LoadAnimation(Animation)
punch:Play()
kEvent:FireServer(Tar.Parent.PrimaryPart)
end
end
Tool.Activated:Connect(function() local suc, err = pcall(SendReq) if not suc then warn(err) end end)
Server script
local Event = game.ReplicatedStorage.Events.KnockbackM
local KnockbackE = game.ReplicatedStorage.Events.GetKnockback
Event.OnServerEvent:Connect(function(Plr, Target)
local Player = game.Players:GetPlayerFromCharacter(Target) or game.Players:GetPlayerFromCharacter(Target.Parent)
if Player then
KnockbackE:FireClient(Player,"Verified")
end
end)
Recipient Local
local KnockBack = game.ReplicatedStorage.Events.GetKnockback
local LPlr = game.Players.LocalPlayer
local Character = LPlr.CharacterAdded:Wait() or LPlr.Character
local Event = KnockBack
local MaxForce = Vector3.new(80000, 80000, 80000)
local Speed = 20
Event.OnClientEvent:Connect(function(Key)
if Key == "Verified" then
if Character and Character:FindFirstChild("HumanoidRootPart") then
local Vel = Instance.new("BodyVelocity")
Vel.Parent = Character.PrimaryPart
Vel.MaxForce = MaxForce
Vel.P = 10
Vel.Velocity = Character.HumanoidRootPart.LookVector * Speed
end
end
end)
I am certain that the order in an or equation does not matter, as in or bit-equations, it would be eithier LPlr.Character or LPlr.CharacterAdded:Wait() depending on what is detected first.