Roblox Lua实现右Ctrl长按/点按切换效果 替换固定7秒自动关闭
方案1:按住右Ctrl生效、松开立即关闭(优先推荐)
该方案完全匹配你的第一需求,同时修复了原代码多次触发会导致移速无限叠加的问题:
local setting = settings().Network local Effect = Instance.new("ColorCorrectionEffect") Effect.Parent = game.Lighting Effect.Saturation = -1 Effect.Brightness = 0 Effect.Contrast = 0 Effect.Enabled = false -- 存储原始移速,避免倍率重复叠加 local originalWalkSpeed = 0 local UserInputService = game:GetService("UserInputService") -- 按下右Ctrl时开启效果 UserInputService.InputBegan:Connect(function(inputObject, gameProcessedEvent) if gameProcessedEvent then return end -- 忽略游戏已处理的输入,比如打字时触发 if inputObject.KeyCode == Enum.KeyCode.RightControl then originalWalkSpeed = game.Players.LocalPlayer.Character.Humanoid.WalkSpeed Effect.Enabled = true setting.IncomingReplicationLag = 1000 game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = originalWalkSpeed * 3 end end) -- 松开右Ctrl时关闭效果 UserInputService.InputEnded:Connect(function(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.RightControl then Effect.Enabled = false setting.IncomingReplicationLag = 0 game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = originalWalkSpeed end end)
方案2:按右Ctrl切换开关状态
如果第一种方案遇到兼容问题,可以使用这个切换模式版本:
local setting = settings().Network local Effect = Instance.new("ColorCorrectionEffect") Effect.Parent = game.Lighting Effect.Saturation = -1 Effect.Brightness = 0 Effect.Contrast = 0 Effect.Enabled = false local isEffectActive = false local originalWalkSpeed = 0 local UserInputService = game:GetService("UserInputService") UserInputService.InputBegan:Connect(function(inputObject, gameProcessedEvent) if gameProcessedEvent then return end if inputObject.KeyCode == Enum.KeyCode.RightControl then isEffectActive = not isEffectActive if isEffectActive then -- 开启效果 originalWalkSpeed = game.Players.LocalPlayer.Character.Humanoid.WalkSpeed Effect.Enabled = true setting.IncomingReplicationLag = 1000 game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = originalWalkSpeed * 3 else -- 关闭效果 Effect.Enabled = false setting.IncomingReplicationLag = 0 game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = originalWalkSpeed end end end)
内容的提问来源于stack exchange,提问作者rocks2260
相关产品推荐
相关产品推荐

