Need help with adding points to leaderstats with DataStore2 in a separate script

Hello, I have a script in workspace under a part that Im trying to get to add a single point when touched. Script worked perfectly using the traditional way of adding points and but cant get it to work with the DataStore2 way (nothing happens when touching the part)

[Edited] fixed some code, get new error saying “UserId is not a valid member of MeshPart”

Here is my script thats supposed to add points after touching

local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)

Berry = script.Parent
berry_sound = game.Workspace.music.berry_collect

Berry.Touched:Connect(function(player)
	local Humanoid = player.Parent:FindFirstChild("Humanoid")
		if Humanoid then
			local Player = game.Players:GetPlayerFromCharacter(player.Parent)
			if Player then
				local berriesStore = DataStore2("berries", player)
				berriesStore:Increment(1,0) -- 0 supposed to be default value, 1 is supposed to add to the default value
				script.Parent.Parent = game.Lighting
				berry_sound:Play()
						

local Pos = math.random(1,3)

if Pos == 1 then
       	script.Parent.Position = Vector3.new(-176.51, 6.777, 1158.025)
  		end
if Pos == 2 then
        script.Parent.Position = Vector3.new(-128.7, 6.777, 1110.945)
  		end
if Pos == 3 then
  		script.Parent.Position = Vector3.new(-128.7, 6.777, 1163.815)
  		end
wait(2)
script.Parent.Parent = game.Workspace

			end
		end
		end)

also, here is the DataStore2 leaderstats script

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local Workspace = game:GetService("Workspace")

local DataStore2 = require(ServerScriptService.DataStore2)

DataStore2.Combine("DATA", "berries", "level", "exp", "bananas")

game.Players.PlayerAdded:Connect(function(player)
	local berriesStore = DataStore2("berries", player)
	local levelStore = DataStore2("level", player)
	local expStore = DataStore2("exp", player)
	local bananasStore = DataStore2("bananas", player)
	
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
 
	local berries = Instance.new("IntValue")
	berries.Name = "Berries"
	berries.Value = berriesStore:Get(0)
	berries.Parent = leaderstats
	
	berriesStore:OnUpdate(function(newBerries)
		berries.Value = newBerries
	end)
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Value = levelStore:Get(1)
	level.Parent = leaderstats
	
	levelStore:OnUpdate(function(newLevel)
		level.Value = newLevel
	end)
	
	local exp = Instance.new("IntValue")
	exp.Name = "EXP"
	exp.Value = expStore:Get(0)
	exp.Parent = leaderstats
	
	expStore:OnUpdate(function(newExp)
		exp.Value = newExp
	end)
	
	local bananas = Instance.new("IntValue")
	bananas.Name = "Bananas"
	bananas.Value = bananasStore:Get(0)
	bananas.Parent = leaderstats
	
	bananasStore:OnUpdate(function(newBananas)
		bananas.Value = newBananas	
	end)
	
	leaderstats.Parent = player
	
end)

Thank you again for any help that will be given. :slight_smile:

Spotted the issue, it took time to find it:

player is infact the Part that hit and Player is the actual Player you got from GetPlayerFromCharacter.
You’re getting this error because the Part that hit was a MeshPart and DataStore2 tried to index UserId on it.

From now on, I suggest you change the parameter name to something such as hit.

I usually do use hit but decided to copy how the DataStore2 github code was written which had player as parameter. I cant believe I missed that. Thank you so much my man!

1 Like