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

Why is my player variable nil?

Asked by 2 years ago
Edited 2 years ago

Hey, I am creating a datastore checker script so I can perform actions to the user's screen. Unfortunately, I am constantly facing an issue where the "plr" variable changes to nil, and I can't seem to find why.

Here is my server script!

local rs = game:GetService("ReplicatedStorage")
local ds = game:GetService("DataStoreService")

local dsActivated = ds:GetDataStore("WindowsActivated")
local newKey = ds:GetDataStore("ProductKey")
local event = rs.licenseKey

local globalPlayerVar

event.OnServerEvent:Connect(function(plr, key)
    globalPlayerVar = plr
    script.Parent.Text = key
    script.Parent.Parent.changeKey.Visible = false
    local success1, errorMessage = pcall(function()
        newKey:SetAsync(plr.UserId.."keyWin", key)
        print(key)
    end)
    if not success1 then
        print(errorMessage)
    end

    local success2, errorMessage2 = pcall(function()
        dsActivated:SetAsync(plr.UserId.."activated", "true")
    end)
    if not success2 then
        print(errorMessage2)
    end

end)

wait(5)

while wait(5) do
    local success3, errorMsg3 = pcall(function()
        license = newKey:GetAsync(globalPlayerVar.UserId.."keyWin")
    end)
    if not success3 then
        print(errorMsg3)
    end

    local success4, errorMsg4 = pcall(function()
        activatedStat = dsActivated:GetAsync(globalPlayerVar.UserId.."activated")
    end)
    if not success4 then
        print(errorMsg4)
    end
end

game.Players.PlayerRemoving:Connect(function(plr2)
    if activatedStat ~= nil then
        if activatedStat == "true" then
            local success, errorMessage = pcall(function()
                dsActivated:SetAsync(plr2.UserId.."activated", "true")
            end)
            if not success then
                print(errorMessage)
            end
        else
            local success, errorMessage = pcall(function()
                dsActivated:SetAsync(plr2.UserId.."activated", "false")
            end)
            if not success then
                print(errorMessage)
            end
        end
    end
end)

Also, I am getting this error in the Output. This could be helpful to fixing my issue.

16:32:55.524 Players.PaleNoobs.PlayerGui.pc.screen.windows.settingsWindow.activation.selectedKey.Script:35: attempt to index nil with 'UserId' - Server - Script:38 16:32:55.524 Players.PaleNoobs.PlayerGui.pc.screen.windows.settingsWindow.activation.selectedKey.Script:42: attempt to index nil with 'UserId' - Server - Script:45

All help is greatly appreciated! -PaleNoobs

Answer this question