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

Help with hovering/flying?

Asked by 8 years ago

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.

debounce = false
player = game.Players.LocalPlayer
b = Instance.new("BodyVelocity")
b.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
b.Velocity = Vector3.new(0,0,0)
mouse = player:GetMouse()
house = script.Parent
b.Parent = house
moving = false
function Talk(msg)
    game:GetService("Chat"):Chat(house, player.Name..": "..msg, Enum.ChatColor.Red)
end

function Aim()
    if debounce == false then
    debounce = true

    local aimer = Instance.new("Part")
    aimer.Transparency = 0
    aimer.Anchored = true
    aimer.CanCollide = false
    aimer.Parent = game.Workspace
    aimer.CFrame = mouse.Hit
    house.CFrame = CFrame.new(house.Position, aimer.Position)
    aimer:Destroy()

    if moving == true then
        b.Velocity = Vector3.new(0,0,0)
    b.Velocity = house.CFrame.lookVector*20
    end
    wait(.1)
    debounce = false
    end
end

mouse.Move:connect(Aim)
mouse.Button1Down:connect(function()
    moving = true
    b.Velocity = house.CFrame.lookVector*20
    mouse.Button1Up:connect(function()
        moving = false
        b.Velocity = Vector3.new(0,0,0)
    end)
end)

player.Chatted:connect(Talk)

1 answer

Log in to vote
1
Answered by
XAXA 1569 Moderation Voter
8 years ago

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:

function Aim()
    if debounce == false then
    debounce = true

    house.CFrame = CFrame.new(house.Position, mouse.Hit.p)

    if moving == true then
        b.Velocity = Vector3.new(0,0,0)
    b.Velocity = house.CFrame.lookVector*20
    end
    wait(.1)
    debounce = false
    end
end

Honestly, this script probably isn't the biggest source of lag. There's something else going on in your game.

0
Thank you so much , I am ignorant of some of the syntax so that helped me alot QuantumToast 261 — 8y
Ad

Answer this question