Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why are these Variables not recognized/nil?

Asked by 2 years ago
Edited 2 years ago

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)

1 answer

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

You've made two connection variables:

  • one is nil (at line 11)
  • one is the 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)
0
Defining connection as nil at line 11 was an attempt at a fix that I found online. Removing it still causes the same outcome brandons2005 87 — 2y
0
That's odd Xapelize 2658 — 2y
0
I'll reread the script Xapelize 2658 — 2y
0
Try printing the connection after defining it, MAYBE it's nil because you disconnected the connection Xapelize 2658 — 2y
View all comments (4 more)
0
Output returns "nil - Server - GainSupplies:12" when asked print(connection) just under the defining of connection brandons2005 87 — 2y
0
alright i've updated the question, see if that works now Xapelize 2658 — 2y
0
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 brandons2005 87 — 2y
0
Fixed the label, I needed to change the .Text of the GUIs in PlayerGUIs, not the StarterGUIs brandons2005 87 — 2y
Ad

Answer this question