Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Object not positioned at mouse when in first person?

Asked by
Troxure 87
5 years ago
Edited 5 years ago

I'm making a tool that is used off of the **Dragger **Instance and for some reason when I try to go in first person or zoomed in to a close perspective of my characters head, the object that is being dragged on the grid is placed elsewhere away from my mouse's position.

GIF: https://gyazo.com/c13e4049d407bc22e9b81390fad043b2

I took the importance of the code and pasted it here, some variables might be missing but they aren't important for understanding, the name should be enough.

Server Side

ObjectToServer.OnServerEvent:Connect(function(_, Id, ObjCF)
    local ModelPlaced = RegisteredObjects[Id]:Clone();
    ModelPlaced.PrimaryPart.CFrame = ObjCF;
    local ModelOwner = Instance.new("ObjectValue", ModelPlaced);
    ModelOwner.Value = _;
    for i,v in next, ModelPlaced:GetDescendants() do
        pcall(function()
            v.CanCollide = true;
            v.Transparency = 0;
        end)
    end
    if(objectIsInBuildingBoundary(_, ModelPlaced)) then
        ModelPlaced.Parent = getOccupiedPlate(_).StoredObjects;
    end
end)

Client Side

local CurrentPlacingModel;
local CurrentPlacingModelID;
local isPlacingObject = false;

local Connection = game:GetService("RunService").RenderStepped;
local Abs = math.abs;

function Module:SelectObject(uniqueId)
    local function Select()
        ClearPlacingObjects(Player);
        local PlayerPlate = getOccupiedPlate(Player);
        local StoredObjects = PlayerPlate.StoredObjects;
        local RegisteredItem = RegisteredItems[uniqueId];
        if(RegisteredItem) then
            CurrentPlacingModelID = uniqueId;
            isPlacingObject = true;
            local outlineObject = RegisteredItems[uniqueId]:Clone();
            outlineObject.Parent, outlineObject.Name = StoredObjects, "PlacingObject";
            outlineObject.PrimaryPart.CFrame = CFrame.new(Mouse.Hit.p);
            local pPart = outlineObject.PrimaryPart;
            for i, Object in next, outlineObject:GetDescendants() do
                pcall(function()
                    Object.Transparency = 0.5;
                end)
            end
            Dragger:MouseDown(pPart, Vector3.new(0, 0, 0), {pPart});
        end
    end
    if(uniqueId ~= CurrentPlacingModelID) then
        if(isPlacingObject) then
            updateCurrentPlacingModel(uniqueId, RegisteredItems[uniqueId]);
        else
            Select();
        end
    else
        Select();
    end
end

function PlaceObject()
    if(isPlacingObject) then
        local PlayerPlate = getOccupiedPlate(Player);
        local StoredObjects = PlayerPlate.StoredObjects;
        local FakeObject = CurrentPlacingModel:Clone();
        local ObjectCF = FakeObject.PrimaryPart.CFrame;
        FakeObject.Name, FakeObject.Parent = "Model#" .. tostring(math.random(1,1000000)), StoredObjects;
        ObjectToServer:FireServer(CurrentPlacingModelID, ObjectCF);
        Dragger:MouseUp();
        FakeObject:Destroy();
        isPlacingObject = false;
        Module:SelectObject(CurrentPlacingModelID);
    end
end

Connection:Connect(function()
    if(isPlacingObject == true) then
        CurrentPlacingModel = getOccupiedPlate(Player).StoredObjects:FindFirstChild("PlacingObject") or false;
        Mouse.TargetFilter = CurrentPlacingModel;
        Dragger:MouseMove(Mouse.UnitRay);
    end
end)

I tried setting the TargetFilter to the object being dragged but that didn't work. I feel like it has something to do with the Mouse.UnitRay but I might be wrong.

Any help would be appreciated, thanks!

0
https://gyazo.com/af9b8b8f9f2875175430143d7e97fe98 is the gyazo incase the one above doesn't open Troxure 87 — 5y

Answer this question