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
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
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
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.
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
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)
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.