im trying to make it so everytime you click a part it drops a money meshpart behind it, and when you touch the meshpart it gives you +1 money on leaderboards with datastores. so far ive almost finished it, but i cant seem to figure out how to make it give you money on the leaderboards. i always get the error code "Workspace.Money.Script:6: attempt to index nil with 'leaderstats'" here is the code for the datastore and the leaderboard:
--datastore local DataStoreService = game:GetService("DataStoreService") local Players = game:getService("Players")
local MoneyData = DataStoreService:GetDataStore("MoneyData")
local CurrencyName = "Money" local StartingValue = 0
Players.PlayerAdded:Connect(function(player) local UserData local success, errMsg = pcall(function() UserData = MoneyData:GetAsync(player.UserId) end)
if success == false then
local doNotSave = Instance.new("Folder")
doNotSave.Name = "DoNotSave"
doNotSave.Parent = player
else
print("Data Is Loaded!!")
end
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Currency = Instance.new("IntValue")
Currency.Name = CurrencyName
Currency.Value = UserData or StartingValue
Currency.Parent = leaderstats
end)
--leaderboard Players.PlayerRemoving:Connect(function(player) local SavingPath = player.leaderstats:FindFirstChild(CurrencyName)
if player:FindFirstChild("DoNotSave") then
warn("Player data was not saved to avoid data loss.")
else
MoneyData:SetAsync(player.UserId, SavingPath.Value)
end
end)
here is the money script:
local Part = script.Parent
Part.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) Player.leaderstats.Money.Value = Player.leaderstats.Money.Value + 1 Part:Destroy() end end)
the datastore script is inside of serverscriptservice and the money script is inside of the money meshpart in replicated storage

4 comments
Player.leaderstats, variable is "player" not "Player"
kk fixed that but it still doesnt work :(
Put the code into a code block.
In the touch function, you made a variable for the Player and named it "player" with a lowercase p, but you did "Player.leaderstats.Money.Value" with an uppercase p. This error happened because Roblox lua is case sensitive and there was no variable named "Player" meaning nil. So your code was basically this "nil.leaderstats.Money.Value" which throws an error.