fix(g4): кнопка по касанию вместо E-нажатия

Проблема:
1. Heartbeat-зов __rbxl_player_x() возвращал константу после первого
   касания кнопки (возможно lua-coroutine / state-issue в Heartbeat).
2. Дверь не открывалась — InputBegan E видимо не доходит или фильтр
   inRange всегда true.

Решение: упростил — кнопка реагирует на Touched (как кнопка-педаль),
без E и без проверки расстояния. Это и понятнее для урока.
Текст подсказки изменён на 'Наступи на красную кнопку'.

Дверь поднимается через TweenService при касании кнопки.
This commit is contained in:
min 2026-06-09 13:39:55 +03:00
parent 39673452cb
commit 4c648d139e

View File

@ -278,7 +278,7 @@ end)`;
g4_main: `-- === ИГРА «КНОПКА-ОТКРЫВАШКА» — главный скрипт (Lua) === g4_main: `-- === ИГРА «КНОПКА-ОТКРЫВАШКА» — главный скрипт (Lua) ===
${SNIPPET_BROADCAST} ${SNIPPET_BROADCAST}
__rbxl_show_text("Подойди к красной кнопке и нажми E", 4) __rbxl_show_text("Наступи на красную кнопку чтобы открыть дверь", 4)
local winSound = Instance.new("Sound", workspace) local winSound = Instance.new("Sound", workspace)
winSound.SoundId = "win"; winSound.Volume = 1 winSound.SoundId = "win"; winSound.Volume = 1
@ -293,69 +293,35 @@ winEvent.Event:Connect(function()
__rbxl_spawn_particles("confetti", px, py + 3, pz, 3, 3) __rbxl_spawn_particles("confetti", px, py + 3, pz, 3, 3)
end)`, end)`,
g4_button: `-- === Скрипт кнопки (Lua) === g4_button: `-- === Скрипт кнопки (Lua) ===
-- Висит на красной кнопке. Реагирует на E когда игрок рядом. -- Висит на красной кнопке. Срабатывает по касанию игрока (как кнопка-педаль).
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players") local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService") local TweenService = game:GetService("TweenService")
local part = script.Parent local part = script.Parent
local player = Players.LocalPlayer
local opened = false local opened = false
local inRange = false
local hintGui = nil
-- Подсказка над кнопкой при подходе -- Подсказка над кнопкой
local function showHint() local hintGui = Instance.new("BillboardGui", part)
if hintGui then return end hintGui.Size = UDim2.new(4, 0, 1, 0)
hintGui = Instance.new("BillboardGui", part) hintGui.StudsOffset = Vector3.new(0, 2, 0)
hintGui.Size = UDim2.new(4, 0, 1, 0) hintGui.AlwaysOnTop = true
hintGui.StudsOffset = Vector3.new(0, 2, 0) local label = Instance.new("TextLabel", hintGui)
hintGui.AlwaysOnTop = true label.Size = UDim2.new(1, 0, 1, 0)
local label = Instance.new("TextLabel", hintGui) label.BackgroundTransparency = 1
label.Size = UDim2.new(1, 0, 1, 0) label.TextColor3 = Color3.fromRGB(255, 255, 255)
label.BackgroundTransparency = 1 label.TextStrokeTransparency = 0
label.TextColor3 = Color3.fromRGB(255, 255, 255) label.TextScaled = true
label.TextStrokeTransparency = 0 label.Text = "Наступи на кнопку"
label.TextScaled = true
label.Text = "[E] Открыть дверь"
end
local function hideHint()
if hintGui then hintGui:Destroy(); hintGui = nil end
end
-- Каждый кадр проверяем расстояние до игрока
local _debugTick = 0
RunService.Heartbeat:Connect(function(dt)
if opened then return end
local px = __rbxl_player_x()
local pz = __rbxl_player_z()
local dx = part.Position.X - px
local dz = part.Position.Z - pz
local dist = math.sqrt(dx*dx + dz*dz)
_debugTick = _debugTick + (dt or 0.016)
if _debugTick > 1 then
_debugTick = 0
print("[g4] px=", px, "pz=", pz, "dist=", dist, "inRange=", inRange)
end
if dist <= 4 and not inRange then
inRange = true
showHint()
elseif dist > 4 and inRange then
inRange = false
hideHint()
end
end)
local clickSound = Instance.new("Sound", part) local clickSound = Instance.new("Sound", part)
clickSound.SoundId = "click"; clickSound.Volume = 0.8 clickSound.SoundId = "click"; clickSound.Volume = 0.8
UserInputService.InputBegan:Connect(function(input, gp) part.Touched:Connect(function(hit)
if gp then return end if opened then return end
if opened or not inRange then return end local h = hit.Parent and hit.Parent:FindFirstChild("Humanoid")
if input.KeyCode ~= Enum.KeyCode.E then return end if not h then return end
opened = true opened = true
hideHint() hintGui:Destroy()
clickSound:Play() clickSound:Play()
__rbxl_show_text("Дверь открывается!", 2) __rbxl_show_text("Дверь открывается!", 2)
part.Color = Color3.fromRGB(100, 255, 100) -- зелёная part.Color = Color3.fromRGB(100, 255, 100) -- зелёная