Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Keybind = Enum.KeyCode.E
- local Players = game:GetService("Players")
- local LocalPlayer = Players.LocalPlayer
- local Workspace = game:GetService("Workspace")
- local StarterGui = game:GetService("StarterGui")
- local RunService = game:GetService("RunService")
- local UIS = game:GetService("UserInputService")
- local PartHolder = Instance.new("Folder",Workspace)
- PartHolder.Name = tostring(math.random(100,100000))
- function CreatePart()
- local Part = Instance.new("Part",PartHolder)
- Part.Name = tostring(math.random(100,100000))
- Part.Anchored = true
- Part.CanCollide = false
- Part.CastShadow = false
- Part.Massless = true
- Part.Position = Vector3.zero
- return Part
- end
- local PredictionBox = CreatePart()
- PredictionBox.Size = Vector3.new(.5,.5,.5)
- PredictionBox.Shape = Enum.PartType.Ball
- local Highlight = Instance.new("Highlight",PredictionBox)
- Highlight.Adornee = PredictionBox
- Highlight.FillTransparency = 0
- Highlight.FillColor = Color3.new(1,0,0)
- Highlight.OutlineTransparency = 0.5
- Highlight.Enabled = true
- local RayPart = CreatePart()
- RayPart.Size = Vector3.new(.1, .1, .1)
- RayPart.Transparency = 1
- local CurrentWeapon
- function Notification(Title,Message,Time)
- StarterGui:SetCore("SendNotification", {
- Title = Title,
- Text = Message or "",
- Duration = Time or 5
- })
- end
- local Success, Error = pcall(function()
- function isMultiPlayer()
- if #Players:GetPlayers() > 1 then
- return true
- end
- return false
- end
- function isHoldingWeapon(GetObject)
- local LocalCharacter = LocalPlayer.Character
- if LocalCharacter then
- for _,Child in pairs(LocalCharacter:GetChildren()) do
- if Child:IsA("Tool") then
- local Path = Child:FindFirstChild("GunServer") or Child:FindFirstChild("KnifeServer")
- if Path then
- local Remote = Path:FindFirstChild("ShootStart") or Path:FindFirstChild("FlingKnife")
- if Remote then
- if GetObject then
- return true, Remote
- else
- return true
- end
- end
- end
- end
- end
- end
- return false
- end
- function isBehindWall(Character)
- local LocalCharacter = LocalPlayer.Character
- if Character and LocalCharacter then
- local LocalRoot = LocalCharacter:FindFirstChild("HumanoidRootPart")
- local OtherRoot = Character:FindFirstChild("HumanoidRootPart")
- if LocalRoot and OtherRoot then
- local Ignored = {LocalCharacter, PredictionBox, RayPart}
- local Ray = Ray.new(LocalRoot.Position,(OtherRoot.Position - LocalRoot.Position).unit * 9e9)
- local Hit = Workspace:FindPartOnRayWithIgnoreList(Ray,Ignored,false,true)
- return Hit and not Hit:IsDescendantOf(Character)
- end
- end
- end
- function isPositionBehindWall(Position, OtherIgnoredInstance)
- if Position then
- local LocalCharacter = LocalPlayer.Character
- if LocalCharacter then
- local LocalRoot = LocalCharacter:FindFirstChild("HumanoidRootPart")
- if LocalRoot then
- RayPart.Position = Position
- local Ignored = {LocalCharacter, OtherIgnoredInstance, PredictionBox}
- local Ray = Ray.new(LocalRoot.Position,(RayPart.Position - LocalRoot.Position).unit * 9e9)
- local Hit = Workspace:FindPartOnRayWithIgnoreList(Ray,Ignored,false,true)
- return Hit and Hit ~= RayPart
- end
- end
- end
- end
- function GetClosestTarget()
- local ClosestTarget
- local ClosestRoot
- local ClosestDistance = math.huge
- if isMultiPlayer() then
- for _,Player in pairs(Players:GetPlayers()) do
- if Player ~= LocalPlayer and Player.Team ~= LocalPlayer.Team then
- local LocalCharacter = LocalPlayer.Character
- local OtherCharacter = Player.Character
- if LocalCharacter and OtherCharacter then
- local LocalRoot = LocalCharacter:FindFirstChild("HumanoidRootPart")
- local OtherRoot = OtherCharacter:FindFirstChild("HumanoidRootPart")
- local OtherHumanoid = OtherCharacter:FindFirstChildOfClass("Humanoid")
- if LocalRoot and OtherRoot and OtherHumanoid and OtherHumanoid.Health > 0 and not isBehindWall(OtherCharacter) then
- local Distance = (LocalRoot.Position - OtherRoot.Position).Magnitude
- if Distance < ClosestDistance then
- ClosestDistance = Distance
- ClosestTarget = Player
- ClosestRoot = OtherRoot
- end
- end
- end
- end
- end
- else
- local Rigs = Workspace:FindFirstChild("Rigs")
- if Rigs and Rigs:IsA("Folder") then
- for _,NpcCharacter in pairs(Rigs:GetChildren()) do
- local LocalCharacter = LocalPlayer.Character
- if NpcCharacter:IsA("Model") and LocalCharacter then
- local LocalRoot = LocalCharacter:FindFirstChild("HumanoidRootPart")
- local NpcRoot = NpcCharacter:FindFirstChild("HumanoidRootPart")
- local NpcHumanoid = NpcCharacter:FindFirstChildOfClass("Humanoid")
- if LocalRoot and NpcRoot and NpcHumanoid and NpcHumanoid.Health > 0 and not isBehindWall(NpcCharacter) then
- local Distance = (LocalRoot.Position - NpcRoot.Position).Magnitude
- if Distance < ClosestDistance then
- ClosestDistance = Distance
- ClosestTarget = NpcCharacter
- ClosestRoot = NpcRoot
- end
- end
- end
- end
- end
- end
- if ClosestTarget and ClosestRoot then
- return ClosestTarget, ClosestRoot
- end
- return nil
- end
- function PredictPosition(Root, PredictType)
- if Root and Root:IsA("BasePart") and PredictType then
- -- local ClosestActualPing = math.floor(LocalPlayer:GetNetworkPing() * 2000)
- local Velocity = Root.Velocity
- local NewVelocity
- local Result
- if string.lower(PredictType) == "gun" then
- NewVelocity = (Velocity * Vector3.new(1, 0, 1)) * (math.random(20, 30) / 100) + Vector3.new(0, Velocity.Y * (math.random(60,80) / 1000), 0)
- elseif string.lower(PredictType) == "knife" then
- local LocalCharacter = LocalPlayer.Character
- if LocalCharacter then
- local LocalRoot = LocalCharacter:FindFirstChild("HumanoidRootPart")
- if LocalRoot then
- local Distance = (LocalRoot.Position - Root.Position).Magnitude
- NewVelocity = (Velocity * Vector3.new(1, 0, 1)) * (math.random(60, 80) / 100) * Distance / 50 + Vector3.new(0, Velocity.Y * (math.random(40,60) / 1000), 0)
- end
- end
- end
- if Root and NewVelocity then
- Result = Root.Position + NewVelocity
- end
- if not isPositionBehindWall(Result, Root.Parent) then
- return Result
- end
- end
- return nil
- end
- task.spawn(function()
- while RunService.Stepped:Wait() do
- if PredictionBox then
- local Target, Root = GetClosestTarget()
- if Target and Root then
- local Position = PredictPosition(Root, CurrentWeapon)
- if Position then
- PredictionBox.Position = Position
- end
- end
- end
- end
- end)
- UIS.InputBegan:Connect(function(Key, Focus)
- if Focus then return end
- if Key.KeyCode == Keybind then
- local boolean, Remote = isHoldingWeapon(true)
- if boolean and Remote then
- if string.find(string.lower(Remote.Parent.Name), "gun") then
- CurrentWeapon = "gun"
- local args = {
- [1] = 1,
- [2] = PredictionBox.Position
- }
- pcall(function() task.spawn(function()
- Remote:FireServer(unpack(args))
- end) end)
- elseif string.find(string.lower(Remote.Parent.Name), "knife") then
- CurrentWeapon = "knife"
- local LocalCharacter = LocalPlayer.Character
- if LocalCharacter then
- local LocalRoot = LocalCharacter:FindFirstChild("HumanoidRootPart")
- if LocalRoot then
- local args = {
- [1] = PredictionBox.CFrame,
- [2] = LocalRoot.Position
- }
- pcall(function() task.spawn(function()
- Remote:FireServer(unpack(args))
- end) end)
- end
- end
- end
- end
- end
- end)
- end)
- if Error then
- Notification("Error",Error,math.huge)
- end
- Notification("Script Loaded.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement