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

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

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by
pwx 1581 Moderation Voter
3 years ago

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)
0
im calling it a studio bug cuz i placed stepped on the very top of the script and got still errored proROBLOXkiller5 112 — 3y
0
i also remembered my script was working before roblox got updated proROBLOXkiller5 112 — 3y
0
came back to say thanks cuz you gave me something to start off with proROBLOXkiller5 112 — 3y
0
No worries pal, have a good one. pwx 1581 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

Answer this question