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

How can I delay my CFrame? [closed]

Asked by
Teeter11 281 Moderation Voter
9 years ago

This must seem like a weird question, but here it goes.

So in my game, my guns are created by CFraming the head of the player to the players mouse. I then CFrame the gun and arms to the head of the player. What im trying to do is delay the CFrame so i wont get the current Frame's CFrame it will be like the last frames CFrame. I dont want to loose the 60 frame speed i currently have with this weapon either. ( just so i dont have a stuttery weapon)

function load()

    Tool = script.Parent
    Player = game.Players.LocalPlayer
    Camera = game.Workspace.CurrentCamera
    Mouse = Player:GetMouse()

    local weld1 = Instance.new("Weld")
    weld1.Parent = Game.JointsService
    weld1.C1 = Player.Character.Head.CFrame:toObjectSpace(Player.Character.Torso.CFrame)
    weld1.Part0 = Player.Character.Torso
    weld1.Part1 = Player.Character.Head

    game:GetService("RunService").RenderStepped:connect(function() --What makes the head aim to the mouse
        weld1.C1 = CFrame.new(Player.Character.Head.CFrame.p, Mouse.Hit.p):toObjectSpace(Player.Character.Torso.CFrame)
    end)

end

wait(2)
load()

This is the script i use that makes the players head look torward their mouse.

Im looking to not run the current CFrame but make it delayed.

Locked by woodengop and YellowoTide

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
1
Answered by
Unclear 1776 Moderation Voter
9 years ago

This is a simple variable manipulation problem!

The idea is to calculate the CFrame for the current frame and store that in a variable. Then, in the next frame you would set the weld's offset to the variable and repeat. This makes sure that you keep your code running alongside the user's framerate.

Your RenderStepped should be fixed to something like this...

local lastCalculation           = weld.C1 -- for the first time RenderStepped runs
game:GetService("RunService").RenderStepped:connect(function()
    local currentCalculation    = CFrame.new() -- do your calculation here
    weld.C1                     = lastCalculation -- set your weld to the last calculation
    lastCalculation             = currentCalculation -- save the calculation for the next frame
end)
Ad
Log in to vote
1
Answered by
woodengop 1134 Moderation Voter
9 years ago

There is a way it's called the Delay function, Here is an Example layout(if you don't Understand the Wiki's layout).

Layout

function printer()--my Function
    print("Boom")
end

delay(5,printer)--this is where the delay gets used.

Now lets add it to your script:

Script Layout

function load()

    Tool = script.Parent
    Player = game.Players.LocalPlayer
    Camera = game.Workspace.CurrentCamera
    Mouse = Player:GetMouse()

    local weld1 = Instance.new("Weld")
    weld1.Parent = Game.JointsService
    weld1.C1 = Player.Character.Head.CFrame:toObjectSpace(Player.Character.Torso.CFrame)
    weld1.Part0 = Player.Character.Torso
    weld1.Part1 = Player.Character.Head

    game:GetService("RunService").RenderStepped:connect(function() --What makes the head aim to the mouse
        weld1.C1 = CFrame.new(Player.Character.Head.CFrame.p, Mouse.Hit.p):toObjectSpace(Player.Character.Torso.CFrame)
    end)

end

delay(2,load)

HOPE THIS HELPED!

0
While delay is a great solution for running a function after a certain amount of time, it doesn't fulfill the requirements listed in this post! He wants to keep the code running according to frame speed. There's no guarantee that delay will wait exactly as long as a frame lasts! Unclear 1776 — 9y
0
Ahh...Thank you. woodengop 1134 — 9y