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