Files
Roblox-Elevator-Game/src/client/Character/Server/Flashlight.lua

108 lines
3.0 KiB
Lua

--!optimize 2
--!native
--!strict
local Flashlight = {}
Flashlight.__index = Flashlight
type UDP = UnreliableRemoteEvent
type FlashlightUDP = RBXScriptConnection
type LocalPlayer = Player
type HumanoidRootPart = BasePart
type EulerXYZ_struct = {number}
Flashlight.Enabled = false
Flashlight.HeadHeight = 1
local CharacterShared = _G.include(script, "CharacterShared")
local Flashlight_UDP = Instance.new("UnreliableRemoteEvent") :: UDP
Flashlight_UDP.Name = "Flashlight"
Flashlight_UDP.Parent = CharacterShared
local FlashlightUDP_Event: FlashlightUDP?
local function FlashlightPart(HumanoidRootPart: HumanoidRootPart): (Part, SpotLight, Sound)
local FlashlightPart = Instance.new("Part") :: Part
FlashlightPart.Size = Vector3.new(.1,.1,.1)
FlashlightPart.CFrame = HumanoidRootPart.CFrame+Vector3.yAxis
FlashlightPart.CanCollide = false
FlashlightPart.CastShadow = false
FlashlightPart.Anchored = true
FlashlightPart.Transparency = 1
local SpotLight = Instance.new("SpotLight") :: SpotLight
SpotLight.Color = Color3.new(1,1,1)
SpotLight.Angle = 80
SpotLight.Range = 30
SpotLight.Brightness = 2
SpotLight.Shadows = true
SpotLight.Enabled = false
SpotLight.Parent = FlashlightPart
local ToggleSound = Instance.new("Sound") :: Sound
ToggleSound.Name = "Flashlight"
ToggleSound.SoundId = "rbxassetid://16454678462"
ToggleSound.Volume = .5
ToggleSound.Parent = HumanoidRootPart
FlashlightPart.Parent = HumanoidRootPart
return FlashlightPart, SpotLight, ToggleSound
end
function Flashlight.constructor(LocalPlayer: LocalPlayer, HumanoidRootPart: HumanoidRootPart)
local FlashlightPart, SpotLight, ToggleSound = FlashlightPart(HumanoidRootPart)
if FlashlightUDP_Event then
warn("[Server Flashlight]: UDP event was already created, duplicating...", debug.traceback())
end
FlashlightUDP_Event = Flashlight_UDP.OnServerEvent:Connect(function(Messenger: Player, CameraEuler: EulerXYZ_struct)
if Messenger.UserId == LocalPlayer.UserId then
if CameraEuler[4] and CameraEuler[4] == 3 then
local RootPartCFrame = HumanoidRootPart.CFrame
FlashlightPart.CFrame = CFrame.new(
RootPartCFrame.X,
RootPartCFrame.Y+Flashlight.HeadHeight,
RootPartCFrame.Z)*CFrame.Angles(CameraEuler[1], CameraEuler[2], CameraEuler[3])
end
else
Messenger:Kick(`"{Messenger.Name}", {Messenger.UserId} r="{Flashlight_UDP.Name}", 1="{tostring(CameraEuler)}"`)
end
end)
return setmetatable({
FlashlightPart = FlashlightPart,
SpotLight = SpotLight,
ToggleSound = ToggleSound
}, Flashlight)
end
function Flashlight:On()
Flashlight.Enabled = true
(self.ToggleSound :: Sound):Play();
(self.SpotLight :: SpotLight).Enabled = Flashlight.Enabled
end
function Flashlight:Off()
Flashlight.Enabled = false
(self.ToggleSound :: Sound):Play();
(self.SpotLight :: SpotLight).Enabled = Flashlight.Enabled
end
function Flashlight:Toggle()
if Flashlight.Enabled then
self:Off()
else
self:On()
end
end
function Flashlight:SetCFrame(CameraCFrame: CFrame)
end
return Flashlight