Problem with the mouse position!?

I want to do a prototype like ‘tower defense simulator’. Where when you buy a thing you can place it with your mouse.

my script (localScript):

player = game.Players.LocalPlayer
local mouse = player:GetMouse()

while true do
wait(0.001)

script.Parent.MouseButton1Click:Connect(function()
local tower = workspace.Tower:Clone()
tower.Parent = workspace

if tower.Locked == false then
tower.Position = mouse.Hit.Position
end

mouse.Button1Down:Connect(function()
tower.Locked = true
end)
end)

end

First, my script is not very good, but also the tower go at different place. When I don’t move my mouse. I want to understand!

thanks!

What do you mean by “Tower go at different place. When I don’t move my mouse.”?

Is the tower not appearing or is it staying in one position or what?

Also you are calling 2 functions which have the same purpose. MouseButton1Down is locking the tower when the button is pressed down and MouseButton1Click fires when the button has made one successful click; the mouse button goes down and up.

So i dont have an exact solution but I can offer assistence in helping you improve it

local players = game:GetService("Players")
local plr = players.LocalPlayer
local mouse = plr:GetMouse()
local runServ = game:GetService("RunService") -- this will help us update every frame

local tower

local function UpdateTower()
      if (not tower) then return end -- cancels if nothing is selected
      tower.Root.CFrame = CFrame.new(mouse.Hit.Position) -- move the CFrame of the root part to move everything -- side note you would want to store the Root as a variable for slightly more optimization

end

local function SelectTower() -- then connect this to whatever selects the tower
      tower = whateverSelectionIs
      other stuff
end



runServ:BindToRenderStep("UpdateTower",Enum.RenderPriority.Camera.Value-1,UpdateTower) -- make it update every frame before the camera is updated

Im gonna be describing stuff you did wrong or is just not good to do
Firstly you didnt store the player variable as a local variable (you should almost always store stuff as local variables)
Second instead of making it update on every frame with RunService you put it in a while true loop with a wait (also wait(.001) has a limit of .03 seconds or 1/30 and wait() is worse than task.wait() i believe since task.wait() is 1/60
third is binding the selection process you have with the MouseButton1Click every time the while loop loops so that can cause performance issues
Fourth the reason it may not be updating correctly is because you updated the position and not the CFrame of it (CFrames is the main positional thing and just updates better than Vector3’s for some reason - if anyone can give me an explanation on this that would be nice since i just know things that are good to do but dont know the reasoning its better behind them)
Fifth and idk if this even counts since its personal guessing but ive never seen anyone use .Locked as a way to stop it from moving usually they would just stop the thing that keeps it moving but i guess it would work

just noticed from what Eathraphobic2 said and when you connect the MouseButton1Click whenever it fires you are also connecting the Button1Down within it instead of having it outside of the MouseButton1Click function

1 Like