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

How do you stop a model from following your mouse?

Asked by 8 years ago
Edited 8 years ago

I'm making a build preview and I need help with the last part of this script. Right now It makes a clone of the object I want to place, and it moves wherever the mouse is then places it when I click on a part named "Allowed1" otherwise it wont place. Anyway, when the mouse is over Allowed1 it places correctly but then it continues to follow the mouse. How would I make the part stop following the mouse and stay where I clicked? Thanks! (Also if you know how I could fix it so it rotates too that would help if not its ok ..)


local player = game.Players.LocalPlayer; local mouse = game.Players.LocalPlayer:GetMouse(); local player = game.Players.LocalPlayer local mouse = player:GetMouse() local b = game.ReplicatedStorage:WaitForChild("Headquarters") local clickdetector = game.Workspace:WaitForChild("Outline"):WaitForChild("ClickDetector") local HQ = b:Clone() target = mouse.Target --------------------------------------------------------------------- --------------------------------------------------------------------- function temp () b:Clone() HQ.Parent = workspace.Buildings for i,child in pairs(HQ:GetChildren()) do if child:IsA("BasePart") then child.Transparency = 0.5 function onKeyDown(key) if child then key = key:lower() --Not working for some reason.. if key == 'r' then child:AxisRotate(Enum.Axis.Y) --Rotate elseif key == 't' then child:AxisRotate(Enum.Axis.Z) end end end end end ------------------------------------------------------ ------------------------------------------------------- mouse.Move:connect(function() local child = workspace.Buildings.Headquarters child:MoveTo(mouse.hit.p) end) mouse.Button1Down:connect(function () mouse.TargetFilter = HQ print (mouse.Target) if mouse.Target ~= nil and mouse.Target.Name == "Allowed1" then -- Where you can build on print "Allowed" print "Placed" for i, child2 in pairs(HQ:GetChildren()) do child2.Transparency = 0 --Set child2 position here....Help end end if mouse.Target.Name == "NotAllowed1" or mouse.Target == nil then --What you dont what the building to be placed on print "Not Allowed" selected = ""; --Doesnt allow it to be placed end; end) end clickdetector.MouseClick:connect (temp)
0
is it a local script or not? DevingDev 346 — 8y
0
Yes its a local script DesertEagle234 55 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

Well, the model you are moving to the position that you clicked is the model that you cloned and are moving. Th code that I wrote below should (hopefully) help you out.

local player = game:GetService("Players").LocalPlayer -- Getting local player
local mouse = player:GetMouse() -- Getting the local player's mouse
mouse.TargetFilter = nil -- setting the TargetFilter to nil, just in case.

local HQ -- This is the variable for the HQ. This will allow us to clone a model and then change it if need easily.
local CanPlace = false -- A denounce to allow the events move the model.

local b = game:GetService("ReplicatedStorage"):FindFirstChild("Headquarters") -- The model
local ClickDetector = game:GetService("Workspace"):FindFirstChild("Outline"):WaitForChild("ClickDetector") -- the click detector


ClickDetector.MouseClicked:connect(function() -- Firing the Click Detector when the clicked.
if not CanPlace then -- Allowing the model to be placed.
CanPlace = true
end
HQ = b:Clone() -- Setting HQ to be a clone of the model
HQ.Parent = game.Workspace.Buildings -- Setting the parent

spawn(function() -- Changing the parts of the cloned model to be transparent.
if game:GetService("Workspace").Buildings:FindFirstChild("Headquarters") then
for I,v in in pairs(HQ:GetChildren()) do
if f:IsA("BasePart") then
v.Transparency = 0.5
end
end)

mouse.Move:connect(function() -- The mouse move function
if CanPlace then -- If the model can be placed.
mouse.TargetFilter = HQ -- Setting the TargetFilter of the mouse.
local Hit  = mouse.Hit.p
HQ:MoveTo(Hit) -- moving the model
end
end)

mouse.Button1Down:connect(function()
if CanPlace then
if mouse.Target ~= nil and mouse.Target.Name == "Allowed1" then -- Is target is allowed and not nil
for I,v in pairs(HQ:GetChildren()) do
if v:IsA("BasePart") then
v.Transparency = 0
end
end
local newModel = HQ:Clone() -- Clones the model to prevent it from moving
newModel.Parent = game.Workspace.Buildings
newModel.Name = "TheDesiredNameHere"

-- Removes any changes done by the script, "reseting" it.
mouse.TargetFilter = nil
HQ.Parent = nil
HQ = nil
CanPlace = false
else --Undoes everything already done. Not sure if this would work.
If HQ then
mouse.TargetFilter = nil
HQ.Parent = nil
HQ = nil
CanPlace = false
end
end
end
end)

Please note that I'm not fully sure that this will work or not, as I'm on Mobile right now. This is basically is the same thing as your script, but will only place the model where it's allowed(aka "Allowed1" and not where else). This clones the model that you placed with a new model, which allows the mouse to stop following the old and unneeded model, and then set it's parent to nil. If the model doesn't appear in the same location as where you clicked, you can easily just set the Position to be the same.

Ad

Answer this question