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

How To Make A Transparent Model Follow your Cursor?

Asked by 6 years ago

So here is a script that does several things when you click a text button. BUT... I have a question! So you know how building games have a gui menu and stuff for placing objects and when you click an object, the GUI closes and a partially transparent model of the selected object follows your cursor. How can I do that? I am not looking for a script but just an explanation for how to do this.

partBlueprint = game.ReplicatedStorage.Part
local frame = script.Parent.Parent.Parent:WaitForChild('Items')
script.Parent.MouseButton1Click:connect(function()
frame.Visible = not frame.Visible
script.Parent.GUI.Disabled = false
script.Parent.Parent.Parent.PlacingPart.Visible = true
end)

2 answers

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
6 years ago

Lets use the :SetPrimaryPartCFrame() function to move the models

First make sure your model has a PrimaryPart set

For the Button1Down event we will check to see if 1) the mouse.target isnt nil 2) the parent of the mouse.target is a model AND that its PRIMARY PART IS SET and 3) mouse.target.parent.parent == workspace . Then setting the target as the mouse.target's parent. adding that model to the targetfilter, and setting down to true

mouse.Button1Down:connect(function()
    if mouse.Target ~= nil then
        if mouse.Target.Parent:IsA("Model") and mouse.Target.Parent.PrimaryPart~=nil and mouse.target.parent.parent==workspace then
            target = mouse.Target.Parent
            mouse.TargetFilter=target
            down = true
        end
    end
end)

For the move event we will check if target isnt nil and down == true. then we will use :SetPrimaryPartCFrame() to move the model

mouse.Move:connect(function()
    if down == true then
        if target ~= nil then
            target:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.p))
        end
    end
end)

Remember we can rotate the model by adding *CFrame.Angles() at the end of CFrame.new()

finally for the button1up event, we will set down to false, target to nill, and clear out the targetfilter

mouse.Button1Up:connect(function()
    down = false
    target = nil
    mouse.TargetFilter = nil
end)

Final form

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local target = nil
local down = nil
local plr = game.Players.LocalPlayer

mouse.Button1Down:connect(function()
    if mouse.Target ~= nil then
        if mouse.Target.Parent:IsA("Model") and mouse.Target.Parent.PrimaryPart~=nil and mouse.target.parent.parent==workspace then
            target = mouse.Target.Parent
            mouse.TargetFilter=target
            down = true
        end
    end
end)

mouse.Move:connect(function()
    if down == true then
        if target ~= nil then
            target:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.p))
        end
    end
end)

mouse.Button1Up:connect(function()
    down = false
    target = nil
    mouse.TargetFilter = nil
end)
0
Thank you for answering but I added this and this made the game very laggy. BunnyFilms1 297 — 6y
0
@BunnyFlims1 I'm guessing your model has many parts? DanzLua 2879 — 6y
0
Before I even loaded in the part, it started lagging the game. And the part has only 1 part. It is a part. BunnyFilms1 297 — 6y
0
@ BunnyFlims1 This script is for models with primary parts like you asked DanzLua 2879 — 6y
Ad
Log in to vote
0
Answered by
Corexty 15
6 years ago

I agree with @DanzLua but I think it's best to use RunService while moving objects instead of using mouse.Move

game:GetService("RunService").RenderStepped:connect(function()
    if down == true then
        if target ~= nil then
            target:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.p))
        end
    end
end)
0
this isn't one of those things where you need to have it run so many times, the event is for efficiency and you wouldn't really see the difference DanzLua 2879 — 6y

Answer this question