Hi there, I am making a tool for my game which is similar to slap battles on Roblox. I want to make a tool like a sword that will hit the player who comes into contact with it away, but every tutorial I follow does not work very well. I am aware that I have to use BodyVelocity and LookVectors but I do not not know how to do so and copy and pasting code is just not helping make the tool better. Any ideas on how I could accomplish this?
Lets say you already have a script for hit detection and you want to add knock back.
Here is some waypoints to guide you through how to make a knock back tool
- you need to get the direction of the forces. We can use CFrame.new(from,to).Look to get the direction of the force with a magnitude of 1
from is the position of the attacker
to is the position of the victim
local Direction = CFrame.new(Attacker.HumanoidRootPart.Position,Victim.HumanoidRootPart.Position)
- Since we have the direction, we cannot just use the direction of the force to apply the forces as the force will be weak as it have a magnitude of 1 or in simpler terms, 1studs/second .
So we got to take direction * Force … The script should look like this
local Direction = CFrame.new(Attacker.HumanoidRootPart.Position,Victim.HumanoidRootPart.Position)
local Force = Direction * 20
- Now , if you want the character to be thrown up in the air while getting knockback, we can add this simple code to the force
local Direction = CFrame.new(Attacker.HumanoidRootPart.Position,Victim.HumanoidRootPart.Position)
local Force = Direction * 20 + Vector3.new(0,25,0)
after you get the force direction and strength , set the victim’s HumanoidRootPart velocity to force
Victim.HumanoidRootPart.Velocity = Force
wow thank you so much! this should really help! would you by any chance be able to give me an efficient script to get hit detection? (I have tried a few but they barely work)
That will be hard but scripting is about learning so I will not give you any script on it . However, I recommend raycasting. Raycasting is basically firing a lazer from a position in a direction
eg)
local Origin = game.Workspace.Part.Position
local Direction = vector3.new(0,1,0)--upwards
local Ray = workspace:Raycast(origin,Direction)
if Ray then
--this basicall means there is a part blocking the ray.
--the variable Ray will thus be set to {Instance = PartBlockingRay , Position = PositionWhere ray hit} there are more variable but I am too lazy to write it down
else
--nothing blocking ray
end
Now lets say you dont want the ray to detect the Attacker . You can add a prams to Whitelist or blacklist a table of objects
Blacklist means the ray will ingore the parts in the table
Whitelist means the ray will only detect parts in the table
now remember this equation for raycasting
Origin = Destination - direction
Direction = Origin - Destination
Destination = Origin+Direction
here is the roblox dev hub post
Now to the sword making process, we need a local script and a server script. You will need a remote event to send the attacker’s mouse position to the server. Got to be honest , Remote event is quite hard to learn
client script
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse
local RemoteEvent = script.Parent:FindFirstChild("RE")
local equip = false
mouse.Button1Down:Connect(function)
RE:FireServer(mouse.Hit.Position)
end)
The server script should look something like this
RE.OnServerEvent:Connect(function(plr,mouseP)
local Origin = plr.Character.Head.Position
local Directon = CFrame.new(Origin,mouseP).LookVector -- get the direction of the attack with a magnitude of 1
local Prams = RaycastPrams.new()
Prams.FilterType = emun.RaycastFilterType.Exclude
Prams.FilterDescendantsInstances = plr.Character:GetChildren()
local Ray = workspace:Raycast(origin,Direction,prams)
if Ray then
--do the checks here to see if it is a humanoid
end
end)
Ticking the solution box for the top post is highly appreciated!!!
thank you so much for your time! I am very happy that anyone replied to my issue so thanks for putting so much thought and effort into your reply!
btw you can just write prams.FilterDecendenceInstance = {plr.Character}
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.