Connection Isn't Connecting

So I’m trying to rewrite an old script I made for this tool that dropped a piano and made it explode, but I’m having trouble with a connection.

The variable pianoMarkConnection dictates when the function markMultiplierVal will run. When the tool is equipped, the pianoMarkConnection is set to a RunService.Heartbeat connection that activates the markMultiplierVal function.

What’s the issue? The issue is the print inside of the markMultiplierVal function is NEVER fired. Checking the pianoMarkConnection’s connected boolean also doesn’t print.

This also causes the tool.Activated function to error, as it says

 Workspace.StarJ3M.Piano.Activate:57: attempt to index nil with 'Disconnect'  -  Client - Activate:57 

I already checked if the tool.Unequipped function is automatically disconnecting the function. But, no it isn’t which is proven by the fact that the print doesn’t run in the output.

What should I do?

local MOUSE_ICON = "rbxasset://textures/GunCursor.png" -- Changes the mouse to the GunCursor Icon.
local RELOADING_ICON = "rbxasset://textures/GunWaitCursor.png" -- Changes the mouse icon to the Gun Wait Curso Icon.

local player = game.Players.LocalPlayer

local tool = script.Parent

local userInputService = game:GetService("UserInputService")
local activateBombRemote = script.Parent:WaitForChild("ActivateBomb")

local pianoMarkMultiplier = 0

local replicatedStorage = game:GetService("ReplicatedStorage")
local pianoModel = replicatedStorage:WaitForChild("Assets"):WaitForChild("Bombs"):WaitForChild("Piano")

local pianoMarkConnection
local pianoMark : Part
local markMesh : SpecialMesh

function markMultiplierVal() -- A connection that when enabled moves the position of the piano mark.
	print(pianoMarkConnection.Connected)
	if userInputService:IsKeyDown(Enum.KeyCode.Y) and pianoMarkMultiplier ~= 500 then
		pianoMarkMultiplier += 1
	elseif userInputService:IsKeyDown(Enum.KeyCode.H) and pianoMarkMultiplier ~= 0 then
		pianoMarkMultiplier -= 1
	end
end

tool.Equipped:Connect(function()
	userInputService.MouseIcon = MOUSE_ICON

	pianoMark, markMesh = activateBombRemote:FireServer("createPianoMark", nil)
	
	repeat task.wait() until pianoMark ~= nil
	
	pianoMark.Transparency = .5
	
	activateBombRemote:FireServer("pianoMarkPosition", pianoMarkMultiplier)

	pianoMarkConnection = game:GetService("RunService").Heartbeat:Connect(markMultiplierVal())
	print(pianoMarkConnection.Connected)
end)

tool.Unequipped:Connect(function()
	print("Tool Has Been Unequipped.")
	pianoMarkConnection:Disconnect()
	pianoMark:Destroy()
	markMesh:Destroy()

	print("Disconnected.")

	userInputService.MouseIcon = MOUSE_ICON
end)

tool.Activated:Connect(function()
	local cooldownTime = activateBombRemote:FireServer("explodePiano", nil)

	pianoMarkConnection:Disconnect()

	repeat task.wait() until cooldownTime == false

	userInputService.MouseIcon = RELOADING_ICON

	userInputService.MouseIcon = MOUSE_ICON
end)

I found the issue I think.

Change this line:

pianoMarkConnection = game:GetService("RunService").Heartbeat:Connect(markMultiplierVal())

to this:

pianoMarkConnection = game:GetService("RunService").Heartbeat:Connect(markMultiplierVal)

Here is the new code. I also added some checks to ensure there aren’t any errors:

local MOUSE_ICON = "rbxasset://textures/GunCursor.png"
local RELOADING_ICON = "rbxasset://textures/GunWaitCursor.png"

local player = game.Players.LocalPlayer
local tool = script.Parent
local userInputService = game:GetService("UserInputService")
local activateBombRemote = script.Parent:WaitForChild("ActivateBomb")
local pianoMarkMultiplier = 0
local replicatedStorage = game:GetService("ReplicatedStorage")
local pianoModel = replicatedStorage:WaitForChild("Assets"):WaitForChild("Bombs"):WaitForChild("Piano")
local pianoMarkConnection
local pianoMark
local markMesh

function markMultiplierVal()
    print(pianoMarkConnection.Connected)
    if userInputService:IsKeyDown(Enum.KeyCode.Y) and pianoMarkMultiplier ~= 500 then
        pianoMarkMultiplier = pianoMarkMultiplier + 1
    elseif userInputService:IsKeyDown(Enum.KeyCode.H) and pianoMarkMultiplier ~= 0 then
        pianoMarkMultiplier = pianoMarkMultiplier - 1
    end
end

tool.Equipped:Connect(function()
    userInputService.MouseIcon = MOUSE_ICON

    pianoMark, markMesh = activateBombRemote:FireServer("createPianoMark", nil)

    repeat task.wait() until pianoMark ~= nil

    pianoMark.Transparency = 0.5

    activateBombRemote:FireServer("pianoMarkPosition", pianoMarkMultiplier)

    pianoMarkConnection = game:GetService("RunService").Heartbeat:Connect(markMultiplierVal)
    print(pianoMarkConnection.Connected)
end)

tool.Unequipped:Connect(function()
    print("Tool Has Been Unequipped.")
    if pianoMarkConnection then
        pianoMarkConnection:Disconnect()
    end
    if pianoMark then
        pianoMark:Destroy()
    end
    if markMesh then
        markMesh:Destroy()
    end

    print("Disconnected.")

    userInputService.MouseIcon = MOUSE_ICON
end)

tool.Activated:Connect(function()
    local cooldownTime = activateBombRemote:FireServer("explodePiano", nil)

    if pianoMarkConnection then
        pianoMarkConnection:Disconnect()
    end

    repeat task.wait() until cooldownTime == false

    userInputService.MouseIcon = RELOADING_ICON

    userInputService.MouseIcon = MOUSE_ICON
end)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.