Flashlight glitching, need help!

Hello, so when I use my flashlight it spazzes out sometimes, and sometimes its fine. I think it has something to do with the tweens updating using renderstepped, if anyone can help fix this issues I would be grateful!

Script:

local cam = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local TS = game:GetService("TweenService")
local Mouse = game.Players.LocalPlayer:GetMouse()
local TI = TweenInfo.new(0.2)
local Flashlight = RS.Flashlight
local Clone = Flashlight:Clone()
Clone.Parent = script.Parent

local spotLight = script.Parent.Parent.Parent.ReplicatedStorage.Flashlight

spotLight.CanCollide = false
spotLight.CanTouch = false
spotLight.CanQuery = false

local SwitchSound = script.Parent.Parent.Parent.ReplicatedStorage.Sounds.Switch

local lightColor = Color3.new(1, 1, 0.882353)
local Brightness = 1
local Angle = 100
local Range = 20
local Transparency = script.Parent.Parent.Parent.ReplicatedStorage.Flashlight
Transparency.Transparency = 1

local Keybind = Enum.KeyCode.Q

local Toggle = false

UIS.InputBegan:Connect(function(Input, p)
	if p then return end
	if Input.KeyCode == Keybind then
		Toggle = not Toggle
		SwitchSound:Play()
	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, Range = Range, Angle = Angle, Color = lightColor}):Play()

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

	end
end)

Video:

My guess to why it happens is that when the player moves fast the CFrames differ too much and when it tries to tween it it spins in place. You should investigate the Cframe.Lookat and see what angles it recieces when moving at high speeds

Another script was interfering with it, I fixed it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.