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

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

Asked by 4 years ago
Edited by royaltoe 4 years ago
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

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)

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 royaltoe 5144 — 4y
0
Thanks again royal! RealKyklops 4 — 4y
0
gucci royaltoe 5144 — 4y
Ad

Answer this question