Error with DataStore2 on increment

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want my DataStore2 to work with incrementing data on multiple datastores

  2. What is the issue? Include screenshots / videos if possible!
    I am getting an error just as in this topic but adding not subtracting: 22:15:23.834 - ServerScriptService.Script:19: attempt to perform arithmetic (sub) on Instance and number

It is : ServerScriptService.DataStore2:191: attempt to perform arithmetic (add) on Instance and number

and I am attaching 2 photos of the error and an f9 during game error.
The errors seem to mainly happen to the first person that joins, but then affects all players eventually.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked in the Dev hub and found the topic I mentioned above but the fix was not the same as what I need to do THAT I KNOW OF, because their fix is that they referenced the object and not the object.Value. I am quite sure I reference the VALUE each time and the datastores are identical with obviously just different names and tags/keys.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

The error comes from what I am posting here below.

local datastore2 = require(game.ServerScriptService:WaitForChild("DataStore2")) --1936396537 game.ServerScriptService:WaitForChild("DataStore2")
local sendScore2plr = game.ReplicatedStorage:WaitForChild("sendScore")
local sendTags2plr = game.ReplicatedStorage:WaitForChild("sendTags")
local defaultScore = 0
local defaultTags = 0
local defaultTagged = 0
local debounce = false--<<not currently used yet

game.Teams.Lobby.PlayerAdded:Connect(function(player)
	wait(5)
	local scoretxt = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui").playerScore
	scoretxt.Text = player:WaitForChild("leaderstats").Score.Value
	print("SCORES UPDATED LOBBY")
end)
	
script.Parent.Handle.Touched:Connect(function(hit)
	--if ball hit player(s)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	--UPDATING THE PLAYER STATS THAT GOT HIT
	if player and player.Team == game.Teams.NOTIT then
		--SEND THE BALL BACK
		if script.Parent.Parent ~= game.Workspace then
			script.Parent.Parent = game.Workspace
			script.Parent.Handle.Velocity = Vector3.new(0,0,0)
			script.Parent.Handle.CFrame = game.Workspace.ballReturn.bottom.CFrame + Vector3.new(0,2,0)
		end
		--UPDATE PLAYER GETTING TAGGED BY BALL
		--local taggedDataStore = datastore2("Tagged", player)
		----for player who got hit
		--taggedDataStore:Increment(1, defaultTagged)
		----callBacks
		--local function taggedUpdated(updatedTagged)
		--	local tagged = player:WaitForChild("leaderstats").Tagged
		--	tagged.Value = taggedDataStore:Get(updatedTagged)
		--end
		--taggedUpdated(defaultTagged)
		--taggedDataStore:OnUpdate(taggedUpdated)
--The above is commented out for now
		
	--UPDATING IT PLAYERS POINTS AND TAGGED OTHER PLAYER POINTS
		for _, plr in pairs(game.Players:GetPlayers()) do
			if plr.Team == game.Teams.IT then
				--POINTS
				local scoreDataStore = datastore2("Score", plr)
				--for player who threw the ball
				scoreDataStore:Increment(1, defaultScore)
				--callback
				local function scoreUpdated(updatedScore)
					local score = plr:WaitForChild("leaderstats").Score
					score.Value = scoreDataStore:Get(updatedScore)
					sendScore2plr:FireClient(plr)
				end
				scoreUpdated(defaultScore)
				scoreDataStore:OnUpdate(scoreUpdated)
				
				
				--TAGS
				local tagsDataStore = datastore2("Tags", plr)
				--for player who threw the ball
				tagsDataStore:Increment(1, defaultTags)
				--callback
				local function tagsUpdated(updatedTags)
					local tags = plr:WaitForChild("leaderstats").Tags
					tags.Value = tagsDataStore:Get(updatedTags)
					sendTags2plr:FireClient(plr)
				end
				tagsUpdated(defaultTags)
				tagsDataStore:OnUpdate(tagsUpdated)
			end
		end		
	end
end)

The “TAGS” datastore is the one it happens to. The score works just fine. The tagsDataStore:Increment(1, DefaultTags) is where it says the error.

I also have that FireClient to player in there to update the player’s GUI text etc so thats just extra. Notice how the score and the tags datastores are written the same?
I have done a LOT of testing and moving things around to get these to work and got the score to work and now I am trying to add the tags, and got this error so I wanted to see if anyone can see something I dont.

servererror

Thank you if you can help me solve this issue. Ill provide anything else needed. Hope all is well

The problem is that the second optional argument to :Increment(), is a default value to replace nil, not “tags”. Also, I’m not exactly sure what “tags” are, but Increment() can only work with numbers.
Screenshot_2020-09-11-20-05-10
What exactly are you trying to do by implementing these tags? If you wanted to store both values in one DataStore, you would probably have to use a table.

The “tags” are just a thing to identify the value just as “score” is to that value. Tag means a player has been “hit” or “tagged”. So, when that happens the player who threw the ball will get a value of 1 added to that leaderstat. So, I have Score, Tags, and Tagged(not yet active, commented out). Score works and Tags doesnt but they are scripted the same. So, tags does try to increment a number, which is an intValue inside a text.

I hope that makes sense so far. tagsDataStore:Increment(1, defaultTags) should work where defaultTags has been set to 0 at the beginning, right?

This is the leaderstats part of them:

--LEADERSTATS
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local score = Instance.new("IntValue")
score.Name = "Score"
score.Value = defaultScore
score.Parent = leaderstats
local scoreDataStore = datastore2("Score", player)	
local score = player:WaitForChild("leaderstats").Score
score.Value = scoreDataStore:Get(player)
local scoretxt = player:WaitForChild("PlayerGui").ScreenGui.playerScore
scoretxt.Text = player:WaitForChild("leaderstats").Score.Value		

local tags = Instance.new("IntValue")
tags.Name = "Tags"
tags.Value = defaultTags
tags.Parent = leaderstats
local tagsDataStore = datastore2("Tags", player)	
local tags = player:WaitForChild("leaderstats").Tags
tags.Value = tagsDataStore:Get(player)
local tagstxt = player:WaitForChild("PlayerGui").ScreenGui.playerTags
tagstxt.Text = player:WaitForChild("leaderstats").Tags.Value

Can you print defaultTags to the output with a print() to make sure that it is the value you think it is, and not a Instance?

Sure thing! Ill try that. Where do you believe I should utilize the print() at in the code to check, shoudl it be before or after the Increment? Thnk you for your help so far

Try putting it right before the Increment(), and see what happens.

Ok, just did and tested. It output: 0

1 Like

I just ran through it 3 times to make sure and Also noticed that player 2 (in test mode and with a live player testing), that the tags did read from the datastore when they joined and updated for them. But still not for the other person playing.

I am reading right now about combining, because you mentioned tables. So I might have to do that because I “think” it might be trying to pull data from both players at the same time as “player” even though I identify them by team first. I was thinking that might have been an issue.

EDIT: In the code, I also identify them different as “player” and “plr” whereas player is the one on the notit team, and plr is the one who is it. Maybe I am mixing things up and not realizing it. :stuck_out_tongue:

These are all good things to edit. Also, are you combining your DataStores? I might just be blind, but I don’t see you using datastore2:Combine().

DataStore2.Combine("DATA", "Tags")

This is a necessity, because not doing this can cause problems later on.

I am actually implimenting it right now :slight_smile: I read that same thing that without it, since it will become default, it will cause issues with multiple datastores and this way itll actually save memory and callbacks etc. So, I am using Datastore2:Combine(“playerData”, “Score”, “Tags”, “Tagged”) to start.
And I believe I can add to it also in the future.
SO, the other thing is I should probably be reading the datastore on update ONLY in the playerAdded or playerJoined function, correct? I noticed in the basic example that the increment is different too so maybe this difference (from what I remember when it first came out[is what I am using now]) is a better way now.

EDIT: I will be doing this for a few days, but if you or anyone has any input on the error I am getting otherwise that may belp too, that would be a LIFESAVER! :stuck_out_tongue: I do believe that the conversation so far has made me think about the process more deeply and notice they did change things a bit. Thank you so far for the insight!

1 Like

I’m sorry I wasn’t able to find the problem. It seems very strange that DataStore2 would throw that error with no Instance involved. Do tell us if you figure out the issue’s cause!

1 Like

I def will :slight_smile: Yeah it confused the heck out of me too, because I have read a LOT and tested a LOT with them already :P. Will update accordingly and hope it helps others! TY!

So far, converting to the combined datastore works for score. I checked it multiple ways, just need to change a bit and then try to do the same for tags to see if a touched event or click event can simple trigger an increase and if so, check the data save and load and then I might be able to say why it didnt work. Will update soon!

FIXED: So, I had read more into the DataStore2 and I was utilizing an older type of scripting for the DS2 that was used when it first started coming into play. I rescripted everything and use the increment code where the properties only have to except the “value”. It works now very well. So, I would only suggest if anyone has issues with datastore 2 saving or loading in game or post load, etc that you use the newest coding they have and make sure everything is up to date.