If your goal is to create something similar to the 'Stamper Tool' then you should have an outline in your mind on how you want to make this and what steps you'll need to take. Remember, gathering your tools before building a house is always an efficient idea. Let's start by listing what we'll need and then talk about why we'll need them and how we'll use them.
1. The model you're working with.
2. The player's mouse
3. The location the mouse is pointing
4. Run Service
5. Boolean value for RunService to check
How do we get these things?
local MyModel = game.ReplicatedStorage.MYMODEL
local ModelClone = nil
local MyPlayer = game:GetService("Players").LocalPlayer
local MyMouse = MyPlayer:GetMouse()
local RunService = game:GetService("RunService")
local PlacementIsAllowed = false
To put it short, we need the model so we can place it, the player so we can find the mouse, the mouse so we can find its location its pointing at, the RunService so we can have smooth operation of our model placement, and the bool value so we can decide when to start/stop moving the object.
Great! Now that we have those variables set up, what do we do with them?
First off, you should have some kind of function that initializes the placement of our model.
This could be clicking on a GUI, walking on a specific part, or some other action initiated by the player.
For this example, I'll write the initializing function, but not what it's triggered by, since that should be up to you. (This is scripting helpers, not scripting writers)
In this following function, we'll need to set up some variables, based off the variables we made in the beginning of this tutorial.
local function Initialize()
ModelClone = MyModel:Clone()
local MyMouse.TargetFilter = ModelClone
local CurrentMousePosition = MyMouse.Hit.p
ModelClone:SetPrimaryPartCFrame(CFrame.new(CurrentMousePosition))
ModelClone.Parent = game.Workspace
PlacementIsAllowed = true
end
Great! Now once you run that function, it should place the model where ever your mouse was. But wait? We want the model to move freely along with the mouse. To do this, we're going to use the RenderStepped event in RunService. This will cause the code to be fired every render frame, which is approximately 1/60th of a second.
RunService.RenderStepped:Connect(function()
if PlacementIsAllowed then
local CurrentMousePosition = MyMouse.Hit.p
ModelClone:SetPrimaryPartCFrame(CFrame.new(CurrentMousePosition))
end
end)
The model should now be freely moving wherever your mouse points.
To make all the parts of the model set to a transparency of 0.5, we'll want to use a loop to iterate through the children of the model. To do this, we'll use a for loop and :GetChildren()
It will look like this:
for _, child in pairs(ModelClone:GetChildren()) do
if child:IsA("BasePart") or child:IsA("Part") then
child.Transparency = 0.5
end
end
However, we don't want that loop to just run at the start of the game, we want it to start at a specific moment. So let's copy that loop into a function.
local function TransparentLoop()
for _, child in pairs(ModelClone:GetChildren()) do
if child:IsA("BasePart") or child:IsA("Part") then
child.Transparency = 0.5
end
end
end
Now we'll want to call that function at the end of our Initialize() function.
Your code should look like this:
local MyModel = game.ReplicatedStorage.MYMODEL
local ModelClone = nil
local MyPlayer = game:GetService("Players").LocalPlayer
local MyMouse = MyPlayer:GetMouse()
local RunService = game:GetService("RunService")
local PlacementIsAllowed = false
local function TransparentLoop()
for _, child in pairs(ModelClone:GetChildren()) do
if child:IsA("BasePart") or child:IsA("Part") then
child.Transparency = 0.5
end
end
end
local function Initialize()
ModelClone = MyModel:Clone()
local MyMouse.TargetFilter = ModelClone
local CurrentMousePosition = MyMouse.Hit.p
ModelClone:SetPrimaryPartCFrame(CFrame.new(CurrentMousePosition))
ModelClone.Parent = game.Workspace
PlacementIsAllowed = true
TransparentLoop()
end
RunService.RenderStepped:Connect(function()
if PlacementIsAllowed then
local CurrentMousePosition = MyMouse.Hit.p
ModelClone:SetPrimaryPartCFrame(CFrame.new(CurrentMousePosition))
end
end)
Then you'll want to come up with some way to turn the placement off. Luckily we created that variable PlacementIsAllowed. All we have to do is set that variable to false and our model will stop following the mouse. Of course, you'll also want to place the actual model, which I won't get into in this answer.
Please note, since the Client cloned() the model, only that player will be able to see it.
This is beneficial when creating 'placement previews', which is what I assume you're making.
Though, when placing the actual object, you'll want to set up a RemoteFunction on a server script that will clone the original item again from ReplicatedStorage and set its CFrame to the exact same as the current clone that is following around your mouse.
Note: I have not tested any of this code and it is purely based off of my scripting experience. If there is a small error or typo, let me know in the comments and I'll correct it.
If this answer helped you and answered your initial question, please leave an upvote and confirm this answer. I hope this helped! Thanks!
1 comment
Magnitude is not that hard, it is just the distance between 2 distances