Здравствуйте! I made a script for a tool that performs knockback on player that was hit by the tool, but it is not working and not showing any errors.
local tool = script.Parent
local Handle = tool.Handle
repeat task.wait(0.5) until tool.Parent.Parent:IsA('Player') -- yields the script until the tool is assigned to a player
local Plr = tool.Parent.Parent
local PlrChar = Plr.Character or Plr.CharacterAdded:Wait()
local Root = PlrChar.HumanoidRootPart
tool.Activated:Connect(function()
local RCP = RaycastParams.new()
RCP.FilterType = Enum.RaycastFilterType.Blacklist
RCP.FilterDescendantsInstances = {workspace.Terrain,PlrChar,tool}
local RCR = workspace:Raycast(Root.Position,Root.CFrame.LookVector,RCP)
if RCR then
local HitP = RCR.Instance
if HitP.Parent:FindFirstChildWhichIsA('Humanoid') or game.Players:GetPlayerFromCharacter(HitP.Parent) then
local Norm = RCR.Normal
HitP:ApplyImpulse(Norm*-2000)
end
end
end)
local players = game:GetService("Players")
local player = script:FindFirstAncestorOfClass("Player")
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local tool = script.Parent
local handle = tool.Handle
local terrain = workspace.Terrain
tool.Activated:Connect(function()
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {terrain, character, tool}
local raycastResult = workspace:Raycast(hrp.Position, hrp.CFrame.LookVector * 100, raycastParams)
if raycastResult then
local hit = raycastResult.Instance
if hit then
local hitModel = hit:FindFirstAncestorOfClass("Model")
if hitModel then
local hitPlayer = players:GetPlayerFromCharacter(hitModel)
if hitPlayer then
--Do code.
end
end
end
end
end)
You just needed to multiply the CFrame’s LookVector by a range, I used a magnitude of 100 (100 studs) for example, I also made some other minor changes/improvements.
Here’s the model file I used to test (it printed successfully when parts were hit).