Two Problems, Beginner Scripter

Hey everyone again! I’ve run into anew problem which the script analysis thing won’t help me fix. (I have no errors) First of all, my DataStore won’t load. I used Alvin’s tutorial , but I’ve learnt today not to trust the guy as it often outdated and wrong?

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

local function onPlayerJoin(Player)
    local leaderstats = Instance.new("Folder",Player)
    leaderstats.Name = "leaderstats"

    cash = Instance.new("IntValue",leaderstats)
    cash.Name = "Cash"
	cash.Value = 0
	
	local data
	local success, errormessage = pcall(function()
    data = cashDataStore:SetAsync(Player.UserId.."-cash")
    end)
	
	if success then
	   cash.Value = data
	   print ("Player Cash Data Successfuly Loaded!")	
	else
		print("There was an error while saving data")
		warn(errormessage)
	end
	
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

local function onPlayerLeave(Player)
local success, errormessage = pcall(function()
      cashDataStore:SetAsync(Player.UserId..("-cash").player.leaderstats.Cash.Value)
end)	
	
	if success then
	   print ("Player Cash Data Successfuly Saved!")	
	else
		print("There was an error while saving data")
		warn(errormessage)
	end
end

game.Players.PlayerRemoving:Connect(onPlayerLeave)

local part = game.Workspace.Part

My second problem is with my script to see if the cash value is working, it’s very basic but I keep getting the error " attempt to perform arithmetic (add) on nil and number"

local part = game.Workspace.Part

local function onTouched()
	cash = cash + 350
end

script.Parent.Touched:Connect(onTouched)

I’d like to know why it doesn’t work? Also do you guys know if TheDevKings tutorials are good? My friend said they taught him how to script. Thanks again! :slightly_smiling_face:

For your second problem, the cash = cash + 350, Its not defining what to add. So its not telling it that its a leaderstat. it’s just telling it to add two values together. I hope that helps! :DDD

What do I say then? charrrrrrrrrr

You have an error here. You are defining it incorrectly. It should be

Player.UserId.."-cash", player.leaderstats.Cash.Value

As for the second problem you didn’t define cash properly. Cash is just an empty variable in the local script so you need to define cash as the players cash.

cash = game.Players.LocalPlayer:WaitForChild('leaderstats'):WaitForChild('Cash')

dunno why the format is so bad but u get what I mean.

Edit: Forgot to mention to update the value of cash in 2nd script.

local function onTouched()
cash.Value = cash.Value + 350
end

I think I do - thanks. charrrrrrrr

Nevermind - it still comes with error message “Argument 2 missing or nil” and the wait for child thing doesn’t work

For the record this is what the script looks like now

local part = game.Workspace.Part
local cash = game.Players.LocalPlayer:WaitForChild(‘leaderstats’):WaitForChild(‘Cash’)

local function onTouched()
cash = cash + 350
end

script.Parent.Touched:Connect(onTouched)

I have 2 fixes.

For setasync, change

player.leaderstats to Player.leaderstats, etc… because it’s case sensitive my fault.

And I said to have the cash.Value update. Cash is just an intvalue but you need to update the actual value.

If you want to check the value you need to specify,

cash.Value = cash.Value + 350 -- can't add an integer to something that is not an integer

If you want to make your code a little bit sweeter, you should just create a variable just so it’s easier if you have to use it multiple times. But this entirely up to you :smile:
local cash2 = 350
cash.Value = cash.Value + cash2

First of all, I don’t see you FETCHING your data in the code. You only use SetAsync. When the player joins use GetAsync instead SetAsync. You also have to change the coins value when the player enters.

TheDevKing’s tutorials are amazing! Taught me how to script. Anyways, on topic. Either 1 or 2. 1: Data Stores don’t work on studio, and only work in game. or

2: You never defined which player it would go into
If anything, you should do this!

game.Players.PlayerAdded:Connect(function(plr)
     onplayerjoin(plr)
end


Also, he mixed it up. He’s supposed to use get async when you join the game, but then set async when they leave