i’m trying to make a knockback script on a tool but that error appears
Server Script Code:
local tool = script.Parent
local remote = game.ReplicatedStorage:WaitForChild("Hitted")
remote.OnServerEvent:Connect(function(plr, target)
if target then
local mouse = plr:GetMouse()
if target.Parent:FindFirstChild("Humanoid") then
local root = target.Parent.HumanoidRootPart
local velocity = Instance.new("BodyVelocity")
velocity.MaxForce = Vector3.new(9999999,9999999,9999999)
velocity.Velocity = CFrame.new(plr.Character.HumanoidRootPart.Position, Vector3, mouse).lookVector * 5
velocity.Parent = root
wait(0.5)
velocity:Destroy()
end
end
end)
Local Script:
local tool = script.Parent
local remote = game.ReplicatedStorage:WaitForChild("Hitted")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
tool.Activated:Connect(function()
remote:FireServer(mouse.Target, true, "Part Equipped")
end)
Player:GetMouse() can only be called from a LocalScript and the server is unable to get the object itself, so you will need to replace your code like this:
Local Script:
local tool = script.Parent
local remote = game.ReplicatedStorage:WaitForChild("Hitted")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
tool.Activated:Connect(function()
remote:FireServer(mouse.Target, mouse.Hit)
end)
Server Script:
local remote = game.ReplicatedStorage:WaitForChild("Hitted")
remote.OnServerEvent:Connect(function(plr, 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")
velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
velocity.P = 1500
velocity.Velocity = lookVector * 5 * Vector3.new(1, 2, 1) --Get mouse direction and increases it, you can mess around with this values until you find a good one
velocity.Parent = root
wait(0.5)
velocity:Destroy()
end
end
end)
local tool = script.Parent
local remote = game.ReplicatedStorage:WaitForChild("Hitted")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
tool.Activated:Connect(function()
remote:FireServer(mouse.Target, mouse.Hit)
end)
Solution Server script code:
local remote = game.ReplicatedStorage:WaitForChild("Hitted")
remote.OnServerEvent:Connect(function(plr, 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")
velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
velocity.P = 1500
velocity.Velocity = lookVector * 5 * Vector3.new(5, 5, 5) --Get mouse direction and increases it, you can mess around with this values until you find a good one
velocity.Parent = root
wait(0.5)
velocity:Destroy()
end
end
end)