How to offset a part in script using relative position

The video should explain what I need to do and so should the script

local cam = workspace.CurrentCamera
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Flashlight = RS.Flashlight
local Clone = Flashlight:Clone()
local flashsound = script["Flashlight Click"] -- flashlight sound under this script

Clone.Parent = script.Parent

local Brightness = 5

local Keybind = Enum.KeyCode.F

local UIS = game:GetService("UserInputService")

local Toggle = false

local Mouse = game.Players.LocalPlayer:GetMouse()

local TS = game:GetService("TweenService")
local TI = TweenInfo.new(.1, Enum.EasingStyle.Sine)


UIS.InputBegan:Connect(function(Input, p)
	if p then return end
	if Input.KeyCode == Keybind then
		flashsound:Play() -- plays the sound on toggle
		Toggle = not Toggle
	end
end)

RunService.RenderStepped:Connect(function()
	if Clone then

		Clone.Position = cam.CFrame.Position
		TS:Create(Clone, TI, {CFrame = CFrame.lookAt(Clone.Position, Mouse.Hit.Position)}):Play()

		if Toggle then
			TS:Create(Clone.SpotLight, TI, {Brightness = Brightness}):Play()

		else
			TS:Create(Clone.SpotLight, TI, {Brightness = 0}):Play()
		end

	end
end)

dont wanna be that guy, but im not really sure what youre trying to do… is the video your code or the desired effect?
if you are working with cframes, you can just multiply CFrames and the second one will be a relative offset of the first

> also oh man im pretty sure you dont need to tween every .RenderStepped, thats 1) horrible for preformance and 2) redundant since renderstepped runs before the screen is updated so youre effectively just setting the cframe with extra overhead.

1 Like

Well, the video is the script, the desired effect is the flashlight model off by like two studs to make like a first person flashlight model effect.

local cam = workspace.CurrentCamera
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Flashlight = RS:WaitForChild("Flashlight")
local Clone = Flashlight:Clone()
local flashsound = script:WaitForChild("Flashlight Click")

Clone.Parent = script.Parent

local Brightness = 5
local Offset = Vector3.new(0, -0.5, -2) -- Offset for flashlight in front of the camera

local Keybind = Enum.KeyCode.F
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(0.1, Enum.EasingStyle.Sine)

local Toggle = false
local Mouse = game.Players.LocalPlayer:GetMouse()

-- Toggle the flashlight with the keybind
UIS.InputBegan:Connect(function(Input, p)
    if p then return end
    if Input.KeyCode == Keybind then
        flashsound:Play()
        Toggle = not Toggle
    end
end)

-- Update flashlight position and rotation relative to the camera
RunService.RenderStepped:Connect(function()
    if Clone then
        -- Position the flashlight in front of the camera with an offset
        local camPos = cam.CFrame.Position
        local camLook = cam.CFrame.LookVector
        local camUp = cam.CFrame.UpVector
        local newPos = camPos + (camLook * Offset.Z) + (camUp * Offset.Y)

        -- Set flashlight position and orientation based on the camera
        Clone.Position = newPos
        TS:Create(Clone, TI, {CFrame = CFrame.lookAt(newPos, Mouse.Hit.Position)}):Play()

        -- Adjust the brightness based on toggle state
        if Toggle then
            TS:Create(Clone.SpotLight, TI, {Brightness = Brightness}):Play()
        else
            TS:Create(Clone.SpotLight, TI, {Brightness = 0}):Play()
        end
    end
end)



You can tweak the Offset variable to control the position of the flashlight relative to the camera.

  • For example, adjusting Vector3.new(0, -1, -3) can move the flashlight lower and further out.