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

"attempt to index nil with UserId" error with data stores?

Asked by 2 years ago

Hello all, I am in need of support. I have been trying for days to find a solution for this error. Basically, the player variable returns nil, and I am literally unable to find why it's nil. I've scoured the internet for answers but most of them are just specific to the people who made the question... anyways, can someone help me fix this dreadful error?

As always with Data Stores, this is a server script.

local dss = game:GetService("DataStoreService")
local WindowsActivated = dss:GetDataStore("WindowsActivated")

local frame = script.Parent
--local t = frame.activateText
local value = script.Parent.Parent.activation.activated

game.Players.PlayerAdded:Connect(function(plrobj)
    plr = game.Players:FindFirstChild(plrobj)
end)

while true do
    wait(0.2)

    local success, errormsg = pcall(function()
        WindowsActivated = WindowsActivated:GetAsync(plr.UserId.."activated")
    end)
    if not success then
        warn(errormsg)
    end
    if WindowsActivated == "true" then
        --t.Visible = false
        frame.mainScrollFrame.Visible = true
    else
        --t.Visible = true
        frame.mainScrollFrame.Visible = false
    end
end

Thanks in advance.

0
use plrobj instead of plr, remove plr entirely, or if you do want to use plr then add plrobj.Name in the parenthesis of line 9 greatneil80 2647 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

There is a problem in line 9 and

If there is more than one player (if you are planning to have 1 player per server then you don't need to), the loop won't loop for the other player. Attempt putting the loop inside of the event

Script for more than one player

local dss = game:GetService("DataStoreService")
local WindowsActivated = dss:GetDataStore("WindowsActivated")

local frame = script.Parent
--local t = frame.activateText
local value = script.Parent.Parent.activation.activated

game.Players.PlayerAdded:Connect(function(plrobj)
    local plr = plrobj -- Here is the problem. Before you were trying to do findfirstchild and you put an instance instead of a string. You can also just do plr line 8 instead of plrobj
    while true do
            wait(0.2)

         local success, errormsg = pcall(function()
                 WindowsActivated = WindowsActivated:GetAsync(plr.UserId.."activated")
         end)
            if not success then
               warn(errormsg)
          end
         if WindowsActivated == "true" then
               --t.Visible = false
              frame.mainScrollFrame.Visible = true
           else
              --t.Visible = true
                frame.mainScrollFrame.Visible = false
         end
    end
end)


Script for one player per server

local dss = game:GetService("DataStoreService")
local WindowsActivated = dss:GetDataStore("WindowsActivated")

local frame = script.Parent
--local t = frame.activateText
local value = script.Parent.Parent.activation.activated
local plr
game.Players.PlayerAdded:Connect(function(plrobj)
    plr = plrobj
end)

while true do
    wait(0.2)

    local success, errormsg = pcall(function()
        WindowsActivated = WindowsActivated:GetAsync(plr.UserId.."activated")
    end)
    if not success then
        warn(errormsg)
    end
    if WindowsActivated == "true" then
        --t.Visible = false
        frame.mainScrollFrame.Visible = true
    else
        --t.Visible = true
        frame.mainScrollFrame.Visible = false
    end
end
0
unfortunately, it didn't work. it still says that the player is nil. in fact, i tried adding a print statement in the PlayerAdded event, and it didn't even run. Is there something blocking it from running? PaleNoobs 37 — 2y
0
my comment just got deleted before i said 'is there any errors?' AProgrammR 398 — 2y
Ad

Answer this question