To modify your script so that it only attacks the player that your sword is facing, you'll need to add a check that determines whether the target player is within a certain angle in front of your sword. This involves calculating the angle between the direction your sword is facing and the direction to the target player. Here's how you can do it:
--// Setting \\--
local range = 15
local maxAngle = 60 -- Maximum angle in degrees to consider a target in front
--// Variable \\--
local player = game:GetService("Players").LocalPlayer
local cosMaxAngle = math.cos(math.rad(maxAngle)) -- Precompute cosine of max angle for efficiency
--// Script \\--
game:GetService("RunService").RenderStepped:Connect(function()
local p = game.Players:GetPlayers()
for i = 1, #p do
local targetPlayer = p[i]
if targetPlayer ~= player then -- Ensure we're not checking ourselves
local character = targetPlayer.Character
if character and character:FindFirstChild("Humanoid") and character.Humanoid.Health > 0 and character:FindFirstChild("HumanoidRootPart") then
if player:DistanceFromCharacter(character.HumanoidRootPart.Position) <= range then
local tool = player.Character and player.Character:FindFirstChildOfClass("Tool")
if tool and tool:FindFirstChild("Handle") then
-- Calculate direction to target and sword's forward vector
local directionToTarget = (character.HumanoidRootPart.Position - tool.Handle.Position).Unit
local toolForwardVector = tool.Handle.CFrame.LookVector
-- Calculate dot product and check if the target is within the acceptable angle
local dotProduct = toolForwardVector:Dot(directionToTarget)
if dotProduct >= cosMaxAngle then
tool:Activate()
for _, part in ipairs(character:GetChildren()) do
if part:IsA("BasePart") then
firetouchinterest(tool.Handle, part, 0)
firetouchinterest(tool.Handle, part, 1)
end
end
end
end
end
end
end
end
end)
Explanation:
- Calculating the Forward Direction:
- We use
tool.Handle.CFrame.LookVector to get the forward direction of your sword.
- Calculating the Direction to the Target:
- We calculate
(character.HumanoidRootPart.Position - tool.Handle.Position).Unit to get the unit vector pointing from your sword to the target player.
- Using the Dot Product:
- The dot product of two unit vectors gives the cosine of the angle between them.
- A dot product close to 1 means the vectors are pointing in the same direction.
- By comparing the dot product to the cosine of the maximum allowed angle, we determine if the target is within that angle in front of the sword.
- Efficiency Improvements:
- Precompute
cosMaxAngle outside of the loop to avoid recalculating it every frame.
- Use
ipairs instead of next or custom indexing loops for better performance.
- Ensuring We're Not Targeting Ourselves:
- Added a check
if targetPlayer ~= player to skip the local player when iterating over all players.
Adjusting the Maximum Angle:
- The
maxAngle variable is set to 60 degrees, meaning the script will attack players within a 60-degree cone in front of your sword.
- You can adjust
maxAngle to make the cone narrower or wider, depending on your preference.
This modification ensures that your script will only attack players who are within a specified angle in front of your sword, simulating the effect of only attacking players that your sword is facing.