Its broken again. I tried your code and it wouldn’t work on mobile or pc. Here’s my current code:
local ServicesModule = require(script.Parent.Parent.ServicesModule)
local CAS = ServicesModule["ContextActionService"]
local UserInputService = ServicesModule["UserInputService"]
local PhysicsService = ServicesModule["PhysicsService"]
local ReplicatedStorage = ServicesModule["ReplicatedStorage"]
local PizzaFollowModule = {}
local GrabObject = nil
local GrabStart = false
local Dragger = nil
local GrabModel = workspace.Pizza
local player = ServicesModule["Players"].LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local camera = game.Workspace.CurrentCamera
-- // Ray table:
local rayTable = {player.Character}
-- // Raycast:
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = rayTable
raycastParams.IgnoreWater = true
local faceCharacter = false -- // face the character when getting dragged
-- // Welding function:
local function weldBetween(a, b)
local weld = Instance.new("ManualWeld", a)
weld.C0 = a.CFrame:inverse() * b.CFrame
weld.Part0 = a
weld.Part1 = b
return weld
end
-- // Collision Function:
local previousCollisionGroups = {}
local function setCollisionGroup(object)
if object:IsA("BasePart") then
previousCollisionGroups[object] = object.CollisionGroupId
PhysicsService:SetPartCollisionGroup(object, "Player Local")
end
end
local function setCollisionGroupRecursive(object)
setCollisionGroup(object)
for _, child in ipairs(object:GetChildren()) do
setCollisionGroupRecursive(child)
end
end
local function resetCollisionGroup(object)
local previousCollisionGroupId = previousCollisionGroups[object]
if not previousCollisionGroupId then return end
local previousCollisionGroupName = PhysicsService:GetCollisionGroupName(previousCollisionGroupId)
if not previousCollisionGroupName then return end
PhysicsService:SetPartCollisionGroup(object, previousCollisionGroupName)
previousCollisionGroups[object] = nil
end
local function onCharacterAdded(character)
setCollisionGroupRecursive(character)
character.DescendantAdded:Connect(setCollisionGroup)
character.DescendantRemoving:Connect(resetCollisionGroup)
end
-- // Pizza Collision:
local function SetPizzaCollision(Model)
for i, v in pairs(Model:GetChildren()) do
if v:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(v, "Pizza Local")
end
end
end
-- // Creating the mover:
function addMover(part)
local newMover = Instance.new("BodyPosition")
newMover.Parent = part
newMover.MaxForce = Vector3.new(40000,40000,40000)
newMover.P = 15000
newMover.D = 1000
newMover.Position = part.Position
newMover.Name = "Mover"
local newRot = Instance.new("BodyGyro")
newRot.Parent = part
newRot.MaxTorque = Vector3.new(3000,3000,3000)
newRot.P = 3000
newRot.D = 500
newRot.CFrame = game.Workspace.CurrentCamera.CFrame
newRot.Name = "RotMover"
local RotOffset = Instance.new("CFrameValue")
RotOffset.Name = "RotOffset"
RotOffset.Parent = part
end
-- // Creating the dragable object
function createDragBall()
local DragBall = Instance.new("Part")
-- // DragBall.BrickColor = BrickColor.new("Electric blue")
-- // DragBall.Material = Enum.Material.Wood
DragBall.Transparency = 1
DragBall.Size = Vector3.new(.2,.2,.2)
DragBall.Shape = "Ball"
DragBall.Name = "DragBall"
DragBall.Parent = workspace
return DragBall
end
function grabObject()
task.wait()
if Dragger ~= nil then
if not table.find(rayTable, Dragger) then
table.insert(rayTable, Dragger)
end
local character = player.Character
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local mouseLocation = UserInputService:GetMouseLocation()
local viewportPointRay = camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = rayTable
local target = workspace:Raycast(viewportPointRay.Origin, viewportPointRay.Direction * 100, raycastParams) or {Position = (viewportPointRay.Origin + viewportPointRay.Direction * 1000)}
local magnitude = (target.Position - humanoidRootPart.CFrame.Position).magnitude
local newCFrame
if faceCharacter then
newCFrame = CFrame.new(Vector3.new(target.Position.X, target.Position.Y + 0, target.Position.Z), humanoidRootPart.Position)
else
newCFrame = CFrame.new(Vector3.new(target.Position.X, target.Position.Y + 0, target.Position.Z))
end
if (humanoidRootPart.CFrame.Position - newCFrame.Position).Magnitude < 10 then
if Dragger:IsA("BasePart") then
Dragger.CFrame = newCFrame
end
else
local direction = (target.Position - humanoidRootPart.Position).Unit
local newPosition = Vector3.new(0.1, 0.1, 0.1) * direction
if faceCharacter then
Dragger.CFrame = CFrame.new(Vector3.new(humanoidRootPart.Position.X + newPosition.X, (humanoidRootPart.Position.Y + newPosition.Y) + 2, humanoidRootPart.Position.Z + newPosition.Z), humanoidRootPart.Position)
else
Dragger.CFrame = CFrame.new(Vector3.new(humanoidRootPart.Position.X + newPosition.X, (humanoidRootPart.Position.Y + newPosition.Y) + 2, humanoidRootPart.Position.Z + newPosition.Z))
end
end
end
end
-- // Grab function:
function Grab(actionName, UserInputState, InputObject)
if actionName == "Grab" then
if UserInputState == Enum.UserInputState.Begin then
-- start grab
local Magnitude = (mouse.Hit.Position - character.Head.CFrame.Position).magnitude
if Magnitude < 10 then
if mouse.Target then
GrabObject = mouse.Target
GrabStart = true
if GrabObject.Name == "Pizza" or GrabObject.Parent.Name == "Pizza" then
print("OBJ")
-- for i, v in pairs(GrabObject.Parent:GetChildren()) do
-- v.CanCollide = false
-- end
end
local DragBall = createDragBall()
DragBall.CFrame = mouse.Hit
Dragger = DragBall
mouse.TargetFilter = GrabObject
local DragBallWeld = weldBetween(DragBall,GrabObject)
addMover(DragBall)
while Dragger do
--Create a ray from the users head to the mouse.
local cf = CFrame.new(character.Head.Position, mouse.Hit.Position)
Dragger.Mover.Position = (cf + (cf.LookVector * 6)).Position
Dragger.RotMover.CFrame = camera.CFrame * CFrame.Angles(Dragger.RotOffset.Value.X,Dragger.RotOffset.Value.Y, Dragger.RotOffset.Value.Z)
task.wait()
end
mouse.TargetFilter = nil
end
end
elseif UserInputState == Enum.UserInputState.End then
if GrabObject ~= nil then
if GrabObject.Name == "Main" then
--for i, v in pairs(GrabObject.Parent:GetChildren()) do
-- v.CanCollide = true
--end
end
end
GrabObject = nil
GrabStart = false
if Dragger then
Dragger:Destroy()
Dragger = nil
end
end
end
end
-- // Mobile grab function:
local function MobileGrab()
local PlayerGUI = player.PlayerGui
local Mouse = player:GetMouse() -- // This works on mobile
local Magnitude = (mouse.Hit.Position - character.Head.CFrame.Position).magnitude
local Button = PlayerGUI.PlaceGUI.Drop
Button.MouseButton1Down:Connect(function()
PlayerGUI.PlaceGUI.Enabled = false
GrabModel.PrimaryPart.LocalPizzaPrompt.Enabled = true
GrabObject = nil
GrabStart = false
if Dragger then
Dragger:Destroy()
Dragger = nil
end
ServicesModule["RunService"]:UnbindFromRenderStep("MovePart")
end)
if Magnitude < 10 then
if mouse.Target then
GrabObject = mouse.Target
GrabStart = true
if GrabObject.Name == "Pizza" or GrabObject.Parent.Name == "Pizza" then
--print("OBJ")
--for i, v in pairs(GrabObject.Parent:GetChildren()) do
-- v.CanCollide = false
--end
end
local DragBall = createDragBall()
DragBall.CFrame = mouse.Hit
Dragger = DragBall
mouse.TargetFilter = GrabObject
local DragBallWeld = weldBetween(DragBall,GrabObject)
addMover(DragBall)
local cf = CFrame.new(character.Head.Position, mouse.Hit.Position)
Dragger.Mover.Position = ((cf + (cf.LookVector * 6)).Position + Vector3.new(0, 2, 0))
Dragger.RotMover.CFrame = camera.CFrame * CFrame.Angles(Dragger.RotOffset.Value.X,Dragger.RotOffset.Value.Y + 2, Dragger.RotOffset.Value.Z)
grabObject()
ServicesModule["RunService"]:BindToRenderStep("MovePart", Enum.RenderPriority.Camera.Value, grabObject)
mouse.TargetFilter = nil
end
end
end
-- // Mobile init function:
local function Mobile_Init()
local PC, Touchscreen = (UserInputService.MouseEnabled and UserInputService.KeyboardEnabled), UserInputService.TouchEnabled
if Touchscreen then
local NewPrompt = Instance.new("ProximityPrompt")
NewPrompt.Name = "LocalPizzaPrompt"
NewPrompt.MaxActivationDistance = 9
NewPrompt.Parent = workspace.Pizza.PrimaryPart
NewPrompt.ClickablePrompt = true
NewPrompt.RequiresLineOfSight = false
NewPrompt.Triggered:Connect(function()
NewPrompt.Enabled = false
player.PlayerGui.PlaceGUI.Enabled = true
workspace.Pizza:SetPrimaryPartCFrame(workspace.Pizza.PrimaryPart.CFrame + Vector3.new(0, 3, 0))
MobileGrab()
end)
else
CAS:BindAction("Grab", Grab, false, Enum.UserInputType.MouseButton1)
end
end
Mobile_Init()
onCharacterAdded(character)
return PizzaFollowModule
Any help would be greatly appreciated. Also I should stress the pc version of this system works I need to fix the mobile version.