Im working on a game where there are these boxes that you can spawn in, you are supposed to control it with your mouse. But some times they get very laggy. Please help.
01 | debounce = false |
02 | player = game.Players.LocalPlayer |
03 | b = Instance.new( "BodyVelocity" ) |
04 | b.MaxForce = Vector 3. new( math.huge , math.huge , math.huge ) |
05 | b.Velocity = Vector 3. new( 0 , 0 , 0 ) |
06 | mouse = player:GetMouse() |
07 | house = script.Parent |
08 | b.Parent = house |
09 | moving = false |
10 | function Talk(msg) |
11 | game:GetService( "Chat" ):Chat(house, player.Name.. ": " ..msg, Enum.ChatColor.Red) |
12 | end |
13 |
14 | function Aim() |
15 | if debounce = = false then |
mouse.Hit
is a CFrame
. It looks like you're doing something convoluted to get the Position
of a CFrame
(Spawning a part at mouse.Hit
and getting the Position
of the part). Really, all you have to do it do mouse.Hit.p
to get the Position
of the CFrame
.
This is what your Aim
function would look like:
01 | function Aim() |
02 | if debounce = = false then |
03 | debounce = true |
04 |
05 | house.CFrame = CFrame.new(house.Position, mouse.Hit.p) |
06 |
07 | if moving = = true then |
08 | b.Velocity = Vector 3. new( 0 , 0 , 0 ) |
09 | b.Velocity = house.CFrame.lookVector* 20 |
10 | end |
11 | wait(. 1 ) |
12 | debounce = false |
13 | end |
14 | end |
Honestly, this script probably isn't the biggest source of lag. There's something else going on in your game.