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)

8 comments
Defining connection as nil at line 11 was an attempt at a fix that I found online. Removing it still causes the same outcome
That's odd
I'll reread the script
Try printing the connection after defining it, MAYBE it's nil because you disconnected the connection
Output returns "nil - Server - GainSupplies:12" when asked print(connection) just under the defining of connection
alright i've updated the question, see if that works now
That fixed Heartbeat, and I was able to do some bugfixing afterwards, and label now print()s correctly, however the text on the label does not update for some reason. I updated the post with my new code
Fixed the label, I needed to change the .Text of the GUIs in PlayerGUIs, not the StarterGUIs