Attemp to index nil with WaitForChild()?

local DataStoreService = game:GetService("DataStoreService")
local moneyCapture = DataStoreService:GetDataStore("moneyCapture")

local function saveData(player)
	
	local Money = player:WaitForChild("leaderstats").Money.Value

ServerScriptService.SaveMoney:6: attempt to index nil with ‘WaitForChild’

API is enabled
This is a server script inside of serverscriptserver

2 Likes

is this all of your code? if so the error is because you dont have a player, which you can get using game.Players.PlayerAdded:Connect(saveData)
if this is not all your code you would need to pass the player argument when calling the function

1 Like

It seems like the player argument in the saveData function is nil? Could you possibly show your full code?

1 Like
local DataStoreService = game:GetService("DataStoreService")
local moneyCapture = DataStoreService:GetDataStore("moneyCapture")

local function saveData(player)
	
	local Money = player.leaderstats.Money.Value

	local success, err = pcall(function()
		moneyCapture:SetAsync(player.UserId, Money)
	end)
	
	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)		
	end
end

changed it up a bit now its just saying leaderstats is nil

2 Likes

im trying to follow this one tutorial just to learn and test out data store btw

did you create a leaderstats. Also did you call the function for saveData?

1 Like

Yes there is a leaderstats and yes i am doing a call for the function after 5 seconds (as a test)

What code is firing the saveData function?

Also you can’t do local Money = player.leaderstats.Money.Value. You have to instead do local Money = player.leaderstats.Money or it will get the old version of the value and you can’t change it in anyway. This is a pretty common mistake so don’t worry. :slight_smile:

1 Like

Okay, i dont really see a problem with your code that you have given. However, where is the code that connects the saveData function to an event? Like for example:

local function saveData(player)
	-- code
end
-- but then where is this part?
game.Players.PlayerAdded:Connect(saveData) -- something like this

Are you creating it on the client? or the server

1 Like

If you created the leaderstats variable and it’s nil, the only possible options are that

  • A: It’s not properly parented to the Player Object

  • B: It’s created on the client (Or from a LocalScript which the server will be unable to detect)

Couldn’t you just try this?

local DataStoreService = game:GetService("DataStoreService")
local moneyCapture = DataStoreService:GetDataStore("moneyCapture")

local function CreateStats(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats" 
    leaderstats.Parent = player
end

local function saveData(player)
	
	local Money = player:WaitForChild("leaderstats"):WaitForChild("Money").Value

	local success, err = pcall(function()
		moneyCapture:SetAsync(player.UserId, Money)
	end)
	
	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(CreateStats)
1 Like
local DataStoreService = game:GetService("DataStoreService")
local moneyCapture = DataStoreService:GetDataStore("moneyCapture")

game.Players.PlayerAdded:Connect(function(player)
	
	local Money = player:WaitForChild("leaderstats").Money.Value

	local success, err = pcall(function()
		moneyCapture:SetAsync(player.UserId, Money)
	end)
	
	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)		
	end
end)

Pretty sure you can figure out what you did wrong.