Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I delay my CFrame?

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.

2 answers

✓ Accepted answer

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)

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!

2 comments

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!

0

Ahh...Thank you.

This discussion is preserved as it was. New answers, comments, and votes are closed.