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

I have an error outputted: attempt to index 'UserId' nil, I've been trying to fix it all day. ?

I've been making a datastore, where I store my data, however, whenever I use 'UserId' it outputs an error. I've seen quite a few posts, and this problem has been occuring mostly to datastores. Are there any solutions? If so, please comment or answer! :)

local DataStoreService = game:GetService("DataStoreService") -- Accessing the targetted service
local PointsStore = DataStoreService:GetDataStore("PointsStore") -- Creating a DataStore; progress will be deleted if key is changed

local Players = game:GetService("Players") -- Accessing the targetted service

function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local Points = Instance.new("IntValue",leaderstats)
	Points.Name = "Points"

	local Rank = Instance.new("StringValue",leaderstats)
	Rank.Name = "Rank"
	
	if player:IsInGroup(5265499) then
		Rank.Value = player:GetRoleInGroup(5265499)
	end
	
	local data 
	local success, errorMessage = pcall(function()
		data = PointsStore:GetAsync("clientPoints"..player.UserId)
	end)
	if success then
		Points.Value = data
	else
		print("There was an error whilst recieving the Client's Data Key")
		warn(errorMessage)
	end
end
game.Players.PlayerAdded:Connect(onPlayerAdded)

function onPlayerRemove(player)
	local success, errorMessage = pcall(function()
		PointsStore:SetAsync("clientPoints"..player.UserId,player.leaderstats.Points.Value)
	end)
	if success then
		print("Successfully recieved Client's Data Key.")
	else
		print("There was an error whilst recieving the Client's Data Key")
		warn(errorMessage)
	end
end
game.Players.PlayerRemoving:Connect(onPlayerRemove)

-- Used for servers which have been shutdown or connection problems
game:BindToClose(function()
	for _,players in pairs(game.Players:GetChildren()) do
		onPlayerRemove()
	end
end)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local getPoints = ReplicatedStorage:WaitForChild("getPoints")


function getPoints(player)
	print(PointsStore:GetAsync(player.UserId.."clientPoints",player.leaderstats.Points.Value))
end
getPoints.OnClientInvoke = getPoints()

1 answer

Check if this works, I've encountered a problem like this before, don't really know why it works.

local DataStoreService = game:GetService("DataStoreService") -- Accessing the targetted service
local PointsStore = DataStoreService:GetDataStore("PointsStore") -- Creating a DataStore; progress will be deleted if key is changed

local Players = game:GetService("Players") -- Accessing the targetted service

game.Players.PlayerRemoving:Connect(function onPlayerAdded(player)
    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

    local Points = Instance.new("IntValue",leaderstats)
    Points.Name = "Points"

    local Rank = Instance.new("StringValue",leaderstats)
    Rank.Name = "Rank"

    if player:IsInGroup(5265499) then
        Rank.Value = player:GetRoleInGroup(5265499)
    end

    local data 
    local success, errorMessage = pcall(function()
        data = PointsStore:GetAsync("clientPoints"..player.UserId)
    end)
    if success then
        Points.Value = data
    else
        print("There was an error whilst recieving the Client's Data Key")
        warn(errorMessage)
    end
end)
function onPlayerRemove(player)
    local success, errorMessage = pcall(function()
        PointsStore:SetAsync("clientPoints"..player.UserId,player.leaderstats.Points.Value)
    end)
    if success then
        print("Successfully recieved Client's Data Key.")
    else
        print("There was an error whilst recieving the Client's Data Key")
        warn(errorMessage)
    end
end
game.Players.PlayerRemoving:Connect(onPlayerRemove)

-- Used for servers which have been shutdown or connection problems
game:BindToClose(function()
    for _,players in pairs(game.Players:GetChildren()) do
        onPlayerRemove()
    end
end)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local getPoints = ReplicatedStorage:WaitForChild("getPoints")


function getPoints(player)
    print(PointsStore:GetAsync(player.UserId.."clientPoints",player.leaderstats.Points.Value))
end
getPoints.OnClientInvoke = getPoints()

1 comment

0

I'm so sorry! It didn't work, perhaps you have any leftover solutions for me to try out? Thanks for helping, though! :D

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