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

How would I go about creating a snap placement system, this is what I have so far?

Asked by 8 years ago

So far I've got an object to move to my mouse location, and place on click, but what I can't seem to figure out is how to get rid of the jittery mess the part looks like as it moves, seems like every time you move your mouse, while the parts moving it goes up 1/5th of a stud, then back down to where it should be, the following code shows what I've done.

local lastHit = nil
local lastTarget = nil
local x,y,z = playerMouse.Hit.x,playerMouse.Hit.y,playerMouse.Hit.z

tool.Equipped:connect(function(eventMouse)
    model = Instance.new("Model", game.Workspace)
    model.Name = "PartM"
    part = Instance.new("Part", game.Workspace.PartM)
    part.Anchored = true
    part.CanCollide = false
    part.Position = eventMouse.Hit.p

    eventMouse.Move:connect(function()
        eventMouse.TargetFilter = eventMouse.Target
        if (playerMouse.Hit ~= lastHit) then
            lastHit = playerMouse.Hit
            lastTarget = playerMouse.Target
            print(lastHit)
            print(lastTarget)

            model:MoveTo(eventMouse.Hit.p)

        end
    end)
end)

1 answer

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

The "jitteriness" that you are experiencing is being cause by setting the mouse's TargetFilter every time the mouse moves. When the TargetFilter is set to a specific part, it gets ignored. Therefore the next time you access the mouse's Target property, it will not include the part set as the TargetFilter. Everytime the mouse moves, you try to reset the mouse's TargetFilter except this time the target is whatever is behind the part you are ignoring. You want to only set the TargetFilter once, to the model you are moving.

Additionally, you are going to want to disconnect the move event or else it will continue to fire even after the tool is unequipped (assuming you haven't already).

local moveEvent

tool.Equipped:connect(function(eventMouse)
    local model = Instance.new("Model", game.Workspace)
    model.Name = "PartM"
    local part = Instance.new("Part", game.Workspace.PartM)
    part.Anchored = true
    part.CanCollide = false
    part.Position = eventMouse.Hit.p

    eventMouse.TargetFilter = model -- Now all descendants of model will be ignored

    moveEvent = eventMouse.Move:connect(function()
        model:MoveTo(eventMouse.Hit.p)
    end)
end)

tool.Unequipped:connect(function()
    if moveEvent then
        moveEvent:disconnect()
    end
end)
0
Had no idea that mouse.move stayed active after the tool was unequipped, thanks for the advice, also thanks for explaining why what I was doing was wrong, I swear i tried it this way before but I guess I did something wrong. Loterman23 10 — 8y
0
No problem man! :) BlackJPI 2658 — 8y
Ad

Answer this question