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
Here is what the money script should look like: First, put a click detector in the part. Inside the click detector, and put this:
local plr = game.Players.LocalPlayer script.Parent.MouseClick:Connect(function() plr.leaderstats.Currency.Value = plr.leaderstats.Currency.Value + 1 end)
I am new to scripting, so I hope. this works. Good luck!!!
As kickoff127 said, you will need a click detector. The script should look like this:
local plr = game.Players.LocalPlayer script.Parent.MouseClick:Connect(function() plr.leaderstats.Currency.Value = plr.leaderstats.Currency.Value + 1 end)
The leaderstats needs to be binded with the object/script.
local Part = script.Parent Part.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) if Player then Player.leaderstats.Money.Value += 1 Part:Destroy() end end end)
What happens is that when something touches the part it fires, it verifys if there is an Humanoid, but it can be finded but not be a player.