So, i have this grabbing system that allows you to pick up obects, and there this bug that allows you to pick up other players with the object that you are holding. And i’m trying to prevent this bug
The issue being is that i don’t know how i could possibly detect if a player is doing a fly glitch or not.
Iv’e tried to use the .touched()
function to detect if its a player’s character to drop the item, which does work, but it creates a new problem; carrying an object and accidentally touching a player, or your self. Which would cause the item to fall out of your hand, which can be very frustrating. Iv’e also tried raycasting under the player, but that too doesn’t seem to work.
How could I solve this?
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
repeat wait() until Player.Character
local Char = Player.Character
local Humanoid = Char:WaitForChild("Humanoid")
local HRP = Char:WaitForChild("HumanoidRootPart")
local PlayerGui = Player:WaitForChild("PlayerGui")
local GrabGui = PlayerGui:WaitForChild("GrabGui")
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local SetNetworkOwnerEvent = ReplicatedStorage:WaitForChild("SetNetworkOwnerEvent")
local Holding = false
local MaxGrabDistance = 10
local Object
local GrabbingForce
local GrabbingGyro
local ObjectDistance
-- Functions
function Grab()
print("Grabbing")
local BodyPos = Instance.new("BodyPosition")
local BodyGyro = Instance.new("BodyGyro")
BodyPos.D = 650
BodyGyro.D = 650
BodyGyro.MaxTorque = Vector3.new(2000, 2000, 2000)
Object = Mouse.Target
SetNetworkOwnerEvent:FireServer(Object, true)
Object.CanGrab.Value = false
GrabbingForce = BodyPos
GrabbingGyro = BodyGyro
GrabbingForce.Name = "GrabbingForce"
GrabbingForce.Parent = Object
GrabbingGyro.Name = "GrabbingGyro"
GrabbingGyro.Parent = Object
ObjectDistance = (Mouse.Target.Position - HRP.Position).Magnitude
Holding = true
end
function Release()
if Holding == true then
print("Releasing")
GrabbingForce:Destroy()
GrabbingGyro:Destroy()
SetNetworkOwnerEvent:FireServer(Object, false)
Object.CanGrab.Value = true
Object = nil
Holding = false
end
end
-- Start Grab
UIS.InputBegan:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseButton1 and Mouse.Target and not Mouse.Target.Locked and Humanoid.Health > 0 then
local magnitude = (Mouse.Target.Position - HRP.Position).Magnitude
local CanGrabValue = Mouse.Target:FindFirstChild("CanGrab")
if Mouse.Target and not Mouse.Target.Locked and magnitude < MaxGrabDistance and not Holding and CanGrabValue and CanGrabValue.Value == true then
Grab()
else
Release()
end
end
end)
-- End Grab
UIS.InputEnded:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
Release()
end
end)
RS.RenderStepped:Connect(function()
-- Grab GUI
if Mouse.Target and not Mouse.Target.Locked then
local magnitude = (Mouse.Target.Position - HRP.Position).Magnitude
if magnitude < MaxGrabDistance and not Holding then
local CanGrabValue = Mouse.Target:FindFirstChild("CanGrab")
if CanGrabValue and CanGrabValue.Value == true then
GrabGui.GrabText.Visible = true
else
GrabGui.GrabText.Visible = false
end
else
GrabGui.GrabText.Visible = false
end
else
GrabGui.GrabText.Visible = false
end
-- Grabbing
if Holding and Object and Object:IsA("BasePart") then
local magnitude = (Object.Position - HRP.Position).Magnitude
if Holding and Object and GrabbingForce and GrabbingGyro and magnitude < (MaxGrabDistance + 5) then
GrabbingForce.Position = Camera.CFrame.Position + (Mouse.UnitRay.Direction * (ObjectDistance + (workspace.CurrentCamera.CoordinateFrame.p - HRP.Position + Vector3.new(0, 1, 0)).magnitude - 2))
GrabbingGyro.CFrame = Object.CFrame
else
Release()
end
-- Anti fly
if Object and Object.Parent then
Object.Touched:Connect(function(hit)
if hit.Parent and hit.Parent:FindFirstChild("Humanoid") and hit.Parent == Char then
Release()
end
end)
end
end
end)
-- Make sure that theres no floating object when the player leaves or dies
Players.PlayerRemoving:Connect(function(player)
if Object and Object.Parent and player == Player then
Release()
end
end)
Humanoid.Died:Connect(function()
if Object and Object.Parent then
Release()
end
end)