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

attempt to index field 'UserId' (a number value)?

local DataStore = game:GetService("DataStoreService")
local PointsData = DataStore:GetOrderedDataStore("Points")

game.Players.PlayerAdded:Connect(function(player)
	local points = PointsData:GetAsync(player.UserId)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local pointsValue = Instance.new("IntValue",leaderstats)
	pointsValue.Value = points
	leaderstats.Parent = player
	pointsValue.Name = "Strength"
end)

game.Players.PlayerRemoving:Connect(function(player)
	local strength = player.leaderstats.Strength.Value
	PointsData:SetAsync(player.UserId.strength)
	print("Saved Data!")
end)

Please help?

1 answer

✓ Accepted answer

user id is a number (the number's user id, mine's 20111088 for example) you are trying to get the player.UserId.strength which literally means '20111088'.Strength.

a number does not have a strength value which is why your script is erroring.

i think you meant to say SetAsync(player.UserId, strength) instead ofSetAsync(player.UserId.strength)

local DataStore = game:GetService("DataStoreService")
local PointsData = DataStore:GetOrderedDataStore("Points")

game.Players.PlayerAdded:Connect(function(player)
    local points = PointsData:GetAsync(player.UserId)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    local pointsValue = Instance.new("IntValue",leaderstats)
    pointsValue.Value = points
    leaderstats.Parent = player
    pointsValue.Name = "Strength"
end)

game.Players.PlayerRemoving:Connect(function(player)
    local strength = player.leaderstats.Strength.Value

	--THIS IS WHAT YOUR CODE SHOULD LOOK LIKE vvvvv
    PointsData:SetAsync(player.UserId, strength)
    print("Saved Data!")
end)

3 comments

0

also formatted your question for you. to format your code, highlight the code you want to format and click the button with the lua logo on it above the textbox

0

Thanks again royal!

0

gucci

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