Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

why am i getting "attempt to index nil with 'Disconnect'"?

these lines of code i tested already and it didnt error at all when i was testing it but when i came back after a few hours of playing games and tested it again it error me

is this a glitch in roblox studio right now or something?because on paper it seems like i did everything right and plus this is the same exact code that worked before and now didnt work

code

local Model = workspace.Model local IsModelRotating_Left = true
local function RotateTheModel_Left()
	Model:SetPrimaryPartCFrame(Model:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,0.00020051119,0))
end

local function RotateTheModel_Right()
	Model:SetPrimaryPartCFrame(Model:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,-0.00020051119,0))
end

local Rotate_Model = game.ReplicatedStorage.ModelRotation_Begin
local HaltModelRotation = game.ReplicatedStorage.ModelRotation_End
local SteppedEvent
local RService = game:GetService("RunService")
Rotate_Model.OnServerEvent:Connect(function()
	SteppedEvent = RService.Stepped:Connect(function()
		if IsModelRotating_Left == true then
			RotateTheModel_Left()
		else
			RotateTheModel_Right()
		end
	end)
end)

HaltModelRotation.OnServerEvent:Connect(function()
	SteppedEvent:Disconnect() --this is the line that got the error
	if IsModelRotating_Left == true then
		IsModelRotating_Left = false
	else
		IsModelRotating_Left = true
	end
end)

2 answers

Because SteppedEvent is inside a function, you can only call it again within' that function. What I'd do is reference it before the function like so:

local Model = workspace.Model 
local IsModelRotating_Left = true
local SteppedEvent = nil -- variable it here, so it can be used in other functions
local function RotateTheModel_Left()
    Model:SetPrimaryPartCFrame(Model:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,0.00020051119,0))
end

local function RotateTheModel_Right()
    Model:SetPrimaryPartCFrame(Model:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,-0.00020051119,0))
end

local Rotate_Model = game.ReplicatedStorage.ModelRotation_Begin
local HaltModelRotation = game.ReplicatedStorage.ModelRotation_End
local SteppedEvent
local RService = game:GetService("RunService")
Rotate_Model.OnServerEvent:Connect(function()
	-- here it obviously changes nil to the actual stepped function
    SteppedEvent = RService.Stepped:Connect(function()
        if IsModelRotating_Left == true then
            RotateTheModel_Left()
        else
            RotateTheModel_Right()
        end
    end)
end)

HaltModelRotation.OnServerEvent:Connect(function()
	-- this should no longer error since this function can now actually see what you're referencing 
    SteppedEvent:Disconnect() --this is the line that got the error
    if IsModelRotating_Left == true then
        IsModelRotating_Left = false
    else
        IsModelRotating_Left = true
    end
end)

4 comments

0

im calling it a studio bug cuz i placed stepped on the very top of the script and got still errored

0

i also remembered my script was working before roblox got updated

0

came back to say thanks cuz you gave me something to start off with

0

No worries pal, have a good one.

i find this a little surpising because this code worked again

local function RotateModel_Left()
	workspace.Model:SetPrimaryPartCFrame(workspace.Model:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,0.025,0))
end

local function RotateModel_Right()
	workspace.Model:SetPrimaryPartCFrame(workspace.Model:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,-0.025,0))
end

local IsRotating_Left = true
local RService = game:GetService("RunService") local Stepped
RemoteEvent.OnServerEvent:Connect(function()
	Stepped = RService.Stepped:Connect(function()
		if IsRotating_Left == true then
			RotateModel_Left()
		else
			RotateModel_Right()
		end
	end)
end)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
	Stepped:Disconnect()
	if IsRotating_Left == true then
		IsRotating_Left = false
	else
		IsRotating_Left = true
	end
end)

i put an end) on the very bottom of my script because i had to copy and paste Another

Stepped = RService Stepped:Connect(function()

between line 10 and 11 and play the game and it some how confused the code into working??!!?!? but the problem is the model is turning turbo fast at sonic speeds and what i did was remove all those code i just wrote before and script worked

however somebody gave me an answer that gives more consistent results

always check if an event exists before trying to disconnect it

if Stepped then
	Stepped:Disconnect()
end

This discussion is preserved as it was. New answers, comments, and votes are closed.