I'm very new to scripting, and I'm trying to create a UI that appears when a proximity prompt is pressed, and which disappears after a few seconds or when the player is 15 studs away, :disconnect()ing the RunService.Heartbeat
The variable "label" defined in line 6 does not print a value when prompted, and does not change when modified by timeLeft
The variable "connection" defined in line 12 gives an error and won't disconnect the Heartbeat: (Fixed)
Workspace.SuppliesContainer.GainSupplies:29: attempt to index nil with 'disconnect' (Fixed)
This is a regular script with the directory Workspace.SuppliesContainer.GainSupplies Everything else in this code appears to work perfectly fine.
local globalVariables = require(game.ReplicatedStorage.GlobalVariables) local ReplicatedStorage = game.ReplicatedStorage local RunService = game:GetService("RunService") local Players = game:GetService("Players") local suppliesContainer = script.Parent local label = game.StarterGui.SuppliesGainGUI.SuppliesGainLabel local function startSuppliesTimer(timeLeft, PlayerTorso, PlayerGui, supplies) local delayEvent = Instance.new("BindableEvent") local connection connection = RunService.Heartbeat:Connect(function(step) print(connection) print(timeLeft) timeLeft -= step local seconds = math.floor(timeLeft % 60) label.Text = string.format("i", seconds) print(label.Text) local distance = (suppliesContainer.Position - PlayerTorso.Position).Magnitude print(distance) if distance > 15 then delayEvent:Fire() connection:disconnect() PlayerGui.SuppliesGainGUI.Enabled = false end if timeLeft <= 0 then delayEvent:Fire() connection:disconnect() while globalVariables.squareProgress ~= 0 do globalVariables.squareProgress = 0 wait(0.01) end globalVariables.squareActive = true label.Text = "Supplies Gathered!" local sound = Instance.new("Sound", game.Workspace) sound.SoundId = "rbxassetid://9120129807" sound.PlayOnRemove=true sound.Volume = 3 sound:Play() if supplies then supplies.Value = supplies.Value + 1 end wait(1) PlayerGui.SuppliesGainGUI.Enabled = false end end) delayEvent.Event:Wait() end workspace.SuppliesContainer.ProximityPrompt.Triggered:Connect(function(playerWhoActivated) local humanoid = playerWhoActivated.Character:FindFirstChildWhichIsA("Humanoid") print(humanoid) if humanoid then local PlayerGui = playerWhoActivated.PlayerGui -- Update the player's leaderboard stat local leaderstats = playerWhoActivated.leaderstats local supplies = leaderstats and leaderstats:FindFirstChild("Supplies") local PlayerTorso = playerWhoActivated.Character.UpperTorso print("should start") PlayerGui.SuppliesGainGUI.Enabled = true startSuppliesTimer(10, PlayerTorso, PlayerGui, supplies) end end)
You've made two connection
variables:
RunService.Heartbeat
connection (at line 12)The connection
in line 29 might accidentally reference the nil value connection, and you can't disconnect a nil value, so it errors
Solution: Replace line 11 & 12 to:
local connection connection = RunService.Heartbeat:Connect(function(step)