Server actions, keybinds TCP, and flashlight UDP

This commit is contained in:
2024-02-26 23:30:26 -05:00
parent 5a4ac9ca03
commit 4d69e259da
6 changed files with 124 additions and 60 deletions

View File

@@ -21,14 +21,15 @@ Actions.CrouchSpeed = .2
Actions.FlashlightEnabled = false Actions.FlashlightEnabled = false
local CharacterShared = _G.include(script, "CharacterShared") local CharacterShared = _G.include(script, "CharacterShared")
local FlashlightRemote = CharacterShared:WaitForChild("Flashlight") local FlashlightRemote: UnreliableRemoteEvent = CharacterShared:WaitForChild("Flashlight")
local Storage = game:GetService("ReplicatedStorage") local Storage: ReplicatedStorage = game:GetService("ReplicatedStorage")
local RS = game:GetService("RunService")
local Tween = require(Storage:WaitForChild("Tween")) local Tween = require(Storage:WaitForChild("Tween"))
local Delta = require(Storage:WaitForChild("Delta")) local Delta = require(Storage:WaitForChild("Delta"))
local CrouchTween = Tween.constructor()
type inherented = any type inherented = any
function Actions.constructor(HumanoidSettings: inherented, Humanoid: Humanoid, CurrentCamera: Camera) function Actions.constructor(HumanoidSettings: inherented, Humanoid: Humanoid, CurrentCamera: Camera)
@@ -51,8 +52,6 @@ function Actions:DisableSneak()
self.HumanoidSettings:SetWalkSpeed(Actions.Walk) self.HumanoidSettings:SetWalkSpeed(Actions.Walk)
end end
local CrouchTween = Tween.constructor()
function Actions:EnableCrouch(StandingWalkSpeed: number) function Actions:EnableCrouch(StandingWalkSpeed: number)
Actions.DoingAction = true Actions.DoingAction = true
Actions.Crouching = true Actions.Crouching = true
@@ -82,7 +81,7 @@ function Actions:EnableFlashlight()
task.spawn(function() task.spawn(function()
while Actions.FlashlightEnabled do while Actions.FlashlightEnabled do
FlashlightRemote:FireServer()
Delta:time() Delta:time()
end end
end) end)

View File

@@ -2,13 +2,14 @@
--!native --!native
--!strict --!strict
local Character = script.Parent.Parent local Dir = script.Parent
local Character = Dir.Parent
local preprocessor = {} local preprocessor = {}
type CharacterSharedFolder = Folder type CharacterSharedFolder = Folder
function preprocessor.CharacterShared(): CharacterSharedFolder function preprocessor.CharacterShared(): CharacterSharedFolder
return Character:WaitForChild("shared") return Dir:WaitForChild("shared")
end end
_G.include = function(this: LuaSourceContainer, FunName: string, ...) _G.include = function(this: LuaSourceContainer, FunName: string, ...)
@@ -21,8 +22,8 @@ _G.include = function(this: LuaSourceContainer, FunName: string, ...)
end end
end end
local Storage = game:GetService("ReplicatedStorage") local Storage: ReplicatedStorage = game:GetService("ReplicatedStorage")
local RS = game:GetService("RunService") :: RunService local RS: RunService = game:GetService("RunService")
local ClientStorage = Storage:WaitForChild("Client") local ClientStorage = Storage:WaitForChild("Client")
local LoadCompleted = ClientStorage:WaitForChild("LoadingComplete") local LoadCompleted = ClientStorage:WaitForChild("LoadingComplete")

View File

@@ -0,0 +1,59 @@
--!optimize 2
--!native
--!strict
local Actions = {}
Actions.__index = Actions
type EventFunction = (...any) -> ()
type ActionsTCP = RBXScriptConnection
local CharacterShared = _G.include(script, "CharacterShared")
local Remote = Instance.new("RemoteEvent") :: RemoteEvent
Remote.Name = "ActionsBind"
Remote.Parent = CharacterShared
local ActionsTCP_Event: ActionsTCP?
function Actions.constructor(LocalPlayer: Player)
local Events: {
[Enum.KeyCode]: EventFunction
} = {}
if ActionsTCP_Event then
warn("[Server Actions]: TCP event was already created, duplicating...", debug.traceback())
end
ActionsTCP_Event = Remote.OnServerEvent:Connect(function(Messenger: Player, Key: Enum.KeyCode)
if Messenger.UserId == LocalPlayer.UserId then
if typeof(Key) == "EnumItem" then
local switch = Events[Key]
if switch then
switch()
end
else
warn(`[Server Actions]: Got an unknown type, Key="{typeof(Key)}" Value="{Key}" from: "{Messenger.Name}"`)
end
end
end)
return setmetatable({
Events = Events,
CurrentActionsTCP_Event = ActionsTCP_Event
}, Actions)
end
function Actions:Add(Key: Enum.KeyCode, f: EventFunction)
self.Events[Key] = f
end
function Actions:Remove(Key: Enum.KeyCode)
self.Events[Key] = nil
if not next(self.Events) then
(self.CurrentActionsTCP_Event :: ActionsTCP):Disconnect()
print("[Server Actions]: ")
end
end
return Actions

View File

@@ -5,68 +5,66 @@
local Flashlight = {} local Flashlight = {}
Flashlight.__index = Flashlight Flashlight.__index = Flashlight
local Remote = Instance.new("UnreliableRemoteEvent") :: UnreliableRemoteEvent Flashlight.Enabled = false
Remote.Name = "Flashlight"
Remote.Parent = _G.include(script, "CharacterShared")
function Flashlight.constructor(Head: BasePart) function Flashlight.constructor(HumanoidRootPart: BasePart)
local FlashlightPart = Instance.new("Part") :: Part local FlashlightPart = Instance.new("Part") :: Part
FlashlightPart.Size = Vector3.new(.1,.1,.1) FlashlightPart.Size = Vector3.new(.1,.1,.1)
FlashlightPart.CFrame = Head.CFrame+Vector3.yAxis FlashlightPart.CFrame = HumanoidRootPart.CFrame
FlashlightPart.CanCollide = false FlashlightPart.CanCollide = false
FlashlightPart.CastShadow = false FlashlightPart.CastShadow = false
FlashlightPart.Anchored = true
FlashlightPart.Transparency = 1 FlashlightPart.Transparency = 1
local SpotLight = Instance.new("SpotLight") :: SpotLight local SpotLight = Instance.new("SpotLight") :: SpotLight
SpotLight.Color = Color3.new(1,1,1) SpotLight.Color = Color3.new(1,1,1)
SpotLight.Angle = 80 SpotLight.Angle = 90
SpotLight.Range = 30 SpotLight.Range = 30
SpotLight.Brightness = 1 SpotLight.Brightness = 1
SpotLight.Shadows = true SpotLight.Shadows = true
SpotLight.Enabled = false SpotLight.Enabled = false
SpotLight.Parent = FlashlightPart SpotLight.Parent = FlashlightPart
local Weld = Instance.new("WeldConstraint") :: WeldConstraint
Weld.Part0 = Head
Weld.Part1 = FlashlightPart
Weld.Parent = FlashlightPart
FlashlightPart.Anchored = false FlashlightPart.Anchored = false
FlashlightPart.Parent = Head FlashlightPart.Parent = HumanoidRootPart
local ToggleSound = Instance.new("Sound") :: Sound local ToggleSound = Instance.new("Sound") :: Sound
ToggleSound.SoundId = "rbxassetid://16454678462" ToggleSound.SoundId = "rbxassetid://16454678462"
ToggleSound.Volume = .5 ToggleSound.Volume = .5
ToggleSound.Parent = Head ToggleSound.Parent = HumanoidRootPart
return setmetatable({ return setmetatable({
FlashlightPart = FlashlightPart,
SpotLight = SpotLight, SpotLight = SpotLight,
ToggleSound = ToggleSound, ToggleSound = ToggleSound
Remote = Remote
}, Flashlight) }, Flashlight)
end end
local FlashlightEnabled = false function Flashlight:On(CameraCFrame: CFrame)
Flashlight.Enabled = true
function Flashlight:On() (self.FlashlightPart :: Part).CFrame *= CameraCFrame;
FlashlightEnabled = true (self.ToggleSound :: Sound):Play();
self.ToggleSound:Play() (self.SpotLight :: SpotLight).Enabled = Flashlight.Enabled
self.SpotLight.Enabled = FlashlightEnabled
end end
function Flashlight:Off() function Flashlight:Off()
FlashlightEnabled = false Flashlight.Enabled = false
self.ToggleSound:Play()
self.SpotLight.Enabled = FlashlightEnabled (self.ToggleSound :: Sound):Play();
(self.SpotLight :: SpotLight).Enabled = Flashlight.Enabled
end end
function Flashlight:Toggle() function Flashlight:Toggle(CameraCFrame: CFrame?)
-- FlashlightEnabled = not FlashlightEnabled if Flashlight.Enabled then
if FlashlightEnabled then
self:Off() self:Off()
else else
self:On() self:On(CameraCFrame :: CFrame)
end end
end end
function Flashlight:SetCFrame(CameraCFrame: CFrame)
end
return Flashlight return Flashlight

View File

@@ -11,6 +11,7 @@ Spine.__index = Spine
type Head = BasePart type Head = BasePart
type UpperTorso = BasePart type UpperTorso = BasePart
type Neck = Motor6D type Neck = Motor6D
type Waist = Motor6D
type NeckC0 = CFrame type NeckC0 = CFrame
type WaistC0 = CFrame type WaistC0 = CFrame
@@ -20,6 +21,7 @@ type struct_Spine = {
Head: Head, Head: Head,
UpperTorso: UpperTorso, UpperTorso: UpperTorso,
Neck: Neck, Neck: Neck,
Waist: Waist,
NeckC0: NeckC0, NeckC0: NeckC0,
WaistC0: WaistC0 WaistC0: WaistC0
} }
@@ -32,9 +34,9 @@ function Spine.constructor(Head: Head, UpperTorso: UpperTorso)
local self = {} :: struct_Spine local self = {} :: struct_Spine
self.Head = Head self.Head = Head
self.UpperTorso = UpperTorso self.UpperTorso = UpperTorso
self.Neck = Head:WaitForChild("Neck") self.Neck = Head:WaitForChild("Neck") :: Neck
self.Waist = UpperTorso:WaitForChild("Waist") self.Waist = UpperTorso:WaitForChild("Waist") :: Waist
self.Remote = Remote self.Remote = Remote :: UnreliableRemoteEvent
self.NeckC0 = self.Neck.C0 self.NeckC0 = self.Neck.C0
self.WaistC0 = self.Waist.C0 self.WaistC0 = self.Waist.C0

View File

@@ -2,17 +2,22 @@
--!native --!native
--!strict --!strict
local Character = script.Parent.Parent local Dir = script.Parent
local Character = Dir.Parent
local preprocessor = {} local preprocessor = {}
local FlashlightCooldown = .10
local Players = game:GetService("Players")
--Create the character shared directory here --Create the character shared directory here
local CharacterShared = Instance.new("Folder") local CharacterShared = Instance.new("Folder")
CharacterShared.Name = "shared" CharacterShared.Name = "shared"
CharacterShared.Parent = Character CharacterShared.Parent = Dir
type CharacterSharedFolder = Folder type CharacterSharedFolder = Folder
function preprocessor.CharacterShared(): CharacterSharedFolder function preprocessor.CharacterShared(): Instance
return CharacterShared return CharacterShared :: CharacterSharedFolder
end end
_G.include = function(this: LuaSourceContainer, FunName: string, ...) _G.include = function(this: LuaSourceContainer, FunName: string, ...)
@@ -26,11 +31,10 @@ _G.include = function(this: LuaSourceContainer, FunName: string, ...)
end end
end end
local Players = game:GetService("Players")
local Shadows = require(script:WaitForChild("Shadows")) local Shadows = require(script:WaitForChild("Shadows"))
local SpineModule = require(script:WaitForChild("SpineKinematics")) local SpineModule = require(script:WaitForChild("SpineKinematics"))
local FlashlightModule = require(script:WaitForChild("Flashlight")) local FlashlightModule = require(script:WaitForChild("Flashlight"))
local ActionsModule = require(script:WaitForChild("Actions"))
local Head = Character:WaitForChild("Head") local Head = Character:WaitForChild("Head")
local UpperTorso = Character:WaitForChild("UpperTorso") local UpperTorso = Character:WaitForChild("UpperTorso")
@@ -40,6 +44,9 @@ local LocalPlayer = Players:GetPlayerFromCharacter(Character)
local CharacterShadows = Shadows.constructor(Character) local CharacterShadows = Shadows.constructor(Character)
local Spine = SpineModule.constructor(Head, UpperTorso) local Spine = SpineModule.constructor(Head, UpperTorso)
local Flashlight = FlashlightModule.constructor(Head) local Flashlight = FlashlightModule.constructor(Head)
local Actions = ActionsModule.constructor(LocalPlayer)
local FlashlightDebounce = false
CharacterShadows:off() --I plan having 2 player support and characters block a ton of light CharacterShadows:off() --I plan having 2 player support and characters block a ton of light
@@ -48,6 +55,16 @@ Character.DescendantAdded:Connect(function(Instance)
CharacterShadows:PartToggle(Instance, false) CharacterShadows:PartToggle(Instance, false)
end) end)
Actions:Add(Enum.KeyCode.F, function()
if not FlashlightDebounce then
Flashlight:Toggle()
FlashlightDebounce = true
task.wait(FlashlightCooldown)
FlashlightDebounce = false
end
end)
Spine.Remote.OnServerEvent:Connect(function(Messenger: Player, CameraPosition: CFrame, IsFirstPerson: boolean) Spine.Remote.OnServerEvent:Connect(function(Messenger: Player, CameraPosition: CFrame, IsFirstPerson: boolean)
if Messenger.UserId == LocalPlayer.UserId then if Messenger.UserId == LocalPlayer.UserId then
if Spine.Enabled then if Spine.Enabled then
@@ -59,16 +76,4 @@ Spine.Remote.OnServerEvent:Connect(function(Messenger: Player, CameraPosition: C
end end
end) end)
local FlashlightDebounce = false _G.include = nil
Flashlight.Remote.OnServerEvent:Connect(function(Messenger: Player)
if Messenger.UserId == LocalPlayer.UserId then
if not FlashlightDebounce then
Flashlight:Toggle()
FlashlightDebounce = true
task.wait(.10)
FlashlightDebounce = false
end
end
end)