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

How do I fix this error: attempt to index nil with 'UserId'?

Asked by 2 years ago

This is the code that I have and I am not sure how to fix it, I have tried switching the type it is in, but that just adds another error.

local YesButton = script.Parent
local sampleDataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")

YesButton.MouseButton1Click:Connect(function(Player)
    local playerKey = "Player_" .. Player.UserId

    local success, val = pcall(function()
        return sampleDataStore:RemoveAsync(playerKey)
    end)

    if success then
        print(val)
    end
end)
1
use game.Players.LocalPlayer.UserId, MouseButton1Click doesn't have any parameters, the word "Player" is just nil greatneil80 2647 — 2y
0
You can't access DataStore in LocalScripts, you should try making a RemoteEvent fire to server and save your data when MouseButton1Click is pressed. You might need to implement a player parameter for the RemoteEvent too Xapelize 2658 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

Just identify the Player as

game.Players.LocalPlayer
Ad

Answer this question