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

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

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)

2 comments

1

use game.Players.LocalPlayer.UserId, MouseButton1Click doesn't have any parameters, the word "Player" is just nil

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

1 answer

Just identify the Player as

game.Players.LocalPlayer

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