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

How do i make my Viewmodel module lag free?

Asked by 6 years ago

I used RunService.RenderStepped because that's the lag free thing i know

My problem is at line 106

Here is the script

local plr = game.Players.LocalPlayer
local mouse = plr:getMouse()
repeat wait(.2) until plr.Character
local char = plr.Character
char:WaitForChild("Humanoid")
local function loadprint(module)
    print(module.." Loaded")
end


--Viewmodel Module
local guns = game.ReplicatedStorage:WaitForChild("Guns")
local viewmodelGun
local diemaus = game.Players.LocalPlayer:getMouse()

diemaus.Icon = "http://www.roblox.com/asset/?id=98386546"
--local animationContent = game:GetService("KeyframeSequenceProvider"):RegisterKeyframeSequence(game.ReplicatedStorage.TestShot)
--script.Animation.AnimationId = animationContent

--game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable

local offset = Vector3.new(0, -.8 ,0.1)
local modeloffset = CFrame.new(0, -.8 ,0.1)
--[[local function SetViewmodelOffset(vector)
    if viewmodelGun then
        --viewmodelGun:SetPrimaryPartCFrame(cframe or game.Workspace.CurrentCamera.CoordinateFrame)
        offset = vector or Vector3.new(0,0,0)   
    end
end]]

local function scan_nocollide(object)
    local obj = object or viewmodelGun
    if obj == nil then return end
    for _,v in pairs(obj:GetChildren()) do
        if v:IsA("BasePart") then
            v.CanCollide = false
        end
        if #v:GetChildren() > 0 then
            scan_nocollide(v)
        end
    end
end

local function ShowWeaponOnScreen(weapon)
    if guns:FindFirstChild(tostring(weapon)) then
        if viewmodelGun then
            viewmodelGun:Destroy()
            viewmodelGun = nil
        end
        if game.Workspace.CurrentCamera:FindFirstChild("Viewmodel") then -- Somehow a second viewmodel was being created when this was called twice, we just see if we created the viewmodel already and if that is the case we destroy the object.
            game.Workspace.CurrentCamera:FindFirstChild("Viewmodel"):Destroy()
            viewmodelGun = nil
        end
        viewmodelGun = guns:FindFirstChild(tostring(weapon)).Model:Clone()
        viewmodelGun.Name = "Viewmodel"
        viewmodelGun.Parent = game.Workspace.CurrentCamera
        scan_nocollide()
    end
end

script.ShowWeaponOnScreen.OnInvoke = ShowWeaponOnScreen



--[[
    print(game.Players.Player1.PlayerGui.ScreenGui.Viewmodel.PlayParticleOnPart:Invoke("MuzzleFlash", game.ReplicatedStorage.Guns.AK47.Resources.Particles.MuzzleFlash, function (name, icle) 
        wait(0.1) 
        icle.Enabled = false
        print("Disable Particle")
    end))
--]]

script.SetViewmodelOffset.OnInvoke = SetViewmodelOffset
script.PlayParticleOnPart.OnInvoke = function (partName, particle, callback)
    if not viewmodelGun then return end
    local icle = particle:Clone()
    icle.Parent = viewmodelGun:FindFirstChild(tostring(partName))
    icle.Enabled = true
    if type(callback) == "function" then
        coroutine.wrap(function ()
            callback(partName, icle)
        end)()
    end
    return icle
end

local delta = 0
local ti, prevTick = tick(), tick()

--SetViewmodelOffset(Vector3.new(1, -0.75, 1.5))
--SetViewmodelOffset(Vector3.new(1,-1.35,-3))

local oldviewmodelCFrame

local function GetViewmodelCFrame()
    if oldviewmodelCFrame then
        return oldviewmodelCFrame:lerp(game.Workspace.CurrentCamera.CoordinateFrame * CFrame.new(offset), 0.35)
    else
        return game.Workspace.CurrentCamera.CoordinateFrame * CFrame.new(offset)
    end
end
local RunSrv = game:GetService("RunService")



RunSrv.RenderStepped:Connect(function()
    if viewmodelGun then
    ti = tick()
    delta = ti - prevTick
    local viewCFrame = GetViewmodelCFrame()
    viewmodelGun:SetPrimaryPartCFrame(viewCFrame  * modeloffset)
    oldviewmodelCFrame = viewmodelGun:GetPrimaryPartCFrame()
prevTick = tick()
end
end)

local function FlickView(vector)
    local cframe = game.Workspace.CurrentCamera.CoordinateFrame * CFrame.Angles(math.rad(vector.x), math.rad(vector.y), math.rad(vector.z))
    game.Workspace.CurrentCamera.CoordinateFrame = cframe
    return game.Workspace.CurrentCamera.CoordinateFrame
end


loadprint("Viewmodel")
0
Don't post your entire script! Just post the relevant parts. NotInventedHere 158 — 6y

Answer this question