I’m trying to make a system in which when equipping the tool the model appears and it follows the position of the player’s mouse, but the model is flying towards the player’s camera, what do I do?
local function onEquipped()
local character = tool.Parent
if character and character:IsA("Model") then
animationTrack = playAnimation(character)
end
local newBalde = Baldao:Clone()
newBalde.Parent = workspace
HasBalde = newBalde
RunService.RenderStepped:Connect(function()
if Mouse.Target then -- Garante que o mouse está apontando para algo
local targetPosition = Mouse.Hit.Position
newBalde:SetPrimaryPartCFrame(CFrame.new(targetPosition))
end
end)
end
local function onUnequipped()
if animationTrack then
animationTrack:Stop()
animationTrack = nil
end
if HasBalde then
HasBalde:Destroy()
end
end
tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)
Your system places the centre of the assembly at the 3D position of the player’s mouse. The 3D position of the player’s mouse is now occupied by that assembly, and its position will be offset to the surface of the assembly. This process repeats, causing the assembly to rapidly inch towards your camera. You need to disregard the assembly from your mouse’s 3D position, which can be done through Mouse.TargetFilter
On another note, Model:SetPrimaryPartCFrame has been deprecated. It was observed to cause the gradual deformation of the assembly over repeated use. Use Model:PivotTo instead.
You’re also going to cause a memory leak by not managing the connection you create to RunService.RenderStepped. Unequipping the tool will not stop your connected function from continuing to update the assembly’s position. Equipping the tool again will re-connect the same function, causing duplication of the process over time. Store the returned RBXScriptConnection object returned by RBXScriptSignal:Connect in an up-value so your onUnequipped
function can invoke RBXScriptConnection:Disconnect
Finally, I recommend not using Instance:IsA to confirm the character is a model solely because the “IsA” method considers subclasses of the given class. For example, the Workspace service inherits from the Model
class, making it technically a Model
. This could allow your tool to be a child of Workspace
in your onEquipped
function. If you want to match a specific class, do so through Instance.ClassName
1 Like