Need Help Getting Rid Of An Error

Hey guys, Army here!
Today, I am going through a DataStore tutorial, when an error popped up and I can’t get rid of it. It doesn’t seem to pop up on the youtube video, and It’s starting to annoy me.

The Code
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("Points")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	local PlayerUserId       = player.UserId
	
	local success, errormessage = pcall(function()
		Data = myDataStore:GetAsync(PlayerUserId)
	end)
	
	if success then
		Cash.Value = Data
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
end)

So since the Data is defined inside a function, making it a local function means that I can’t access it outside of the function. I remove the local part, but now there’s an error recommending that I use local, but I can’t use local due to needing it outside of the function.
Can someone please help me fix this? Errors bug me but there doesnt seem to be a way to fix this one.

Are you trying to set the Cash.Value in leaderstats, but there’s an error that says that “Data” variable doesn’t exist?

it’s not really an error, its just some sort of warning, but if its bothers you that much you can do this

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("Points")
local Data

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	local PlayerUserId       = player.UserId
	
	local success, errormessage = pcall(function()
		Data = myDataStore:GetAsync(PlayerUserId)
	end)
	
	if success then
		Cash.Value = Data
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
end)

The error is underlining Data on line 17, it says “W003: Global ‘Data’ is only used in the enclosing function defined at line 4: consider changing it to local”

local success, errormessage = pcall(function()
	Data = myDataStore:GetAsync(PlayerUserId)
end)

Instead of declaring a variable, you could instead just set the value in the leaderstads in this function like this:

local success, errormessage = pcall(function()
	Cash.Value = myDataStore:GetAsync(PlayerUserId)
end)

And if there’s an error, you could just catch it and do everything you want with it.

1 Like

Thank you, errors or warnings kind of just feel annoying and I want to get rid of it, maybe it’s just me though.

1 Like

It’s not just you. If you start abandoning bugs instead of squashing them, you run into problems later.

In this case, you need to declare the variable in the smallest block of code that encompasses all references to that variable (or higher).

“Data” is used in the PlayerAdded block and the pcall (which is in the PlayerAdded block). You can get away with defining Data in the PlayerAdded block. Of course, defining it up top also includes both references, so this works too.

1 Like