Code issues - intValue in my player's folder does not increment

Hello! In my player’s folder, I have a folder named PlayerData, and in this folder, I have an intValue named CB. I would like it to increment when the command is given, but it’s not working. Any ideas on what could be causing this issue?

image

server-side script (At the end of the script, the increment of this int value is done):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local deliverOrderFunction = ReplicatedStorage.remoteFunctions:WaitForChild("deliverOrderFunction")

local deliveryAreasFolder = game.Workspace.Game_Elements.Deliverables.CheffDeliver_Area.deliverAreasBoxes

local function findNearestDeliveryArea(playerPosition)
	local nearestBox = nil
	local shortestDistance = math.huge

	for _, box in ipairs(deliveryAreasFolder:GetChildren()) do
		local boxPosition = box.Position
		local distance = (playerPosition - boxPosition).Magnitude

		if distance < shortestDistance then
			nearestBox = box
			shortestDistance = distance
		end
	end

	return nearestBox
end

local function resetToolAnchoredStatus(toolClone)
	toolClone.Handle.Anchored = false -- Cambiar Anchored a false cuando el jugador recoja la herramienta
end

local function resetDeliveryAreaTransparency(toolClone, deliveryBox)
	toolClone.Equipped:Connect(function()
		deliveryBox.Transparency = 0
		wait(0.2)
		resetToolAnchoredStatus(toolClone) -- Pasar la herramienta clonada como argumento
	end)
end

local function cloneTool(tool, destination)
	local toolClone = tool:Clone()
	toolClone.Parent = destination
	toolClone.Handle.Position = destination.Position
	toolClone.Handle.Anchored = true -- Fijar Anchored en true al clonar la herramienta
	return toolClone
end

deliverOrderFunction.OnServerInvoke = function(player, orderData)
	local playerPosition = player.Character and player.Character.HumanoidRootPart.Position
	if not playerPosition then
		return false, "Player position not available"
	end

	local playerTools = player.Backpack:GetChildren()

	local item1Found = false
	local item2Found = false

	local nearestBox1 = nil
	local nearestBox2 = nil

	for _, tool in ipairs(playerTools) do
		if tool.Name == orderData.item1 then
			item1Found = true
		elseif orderData.item2 and tool.Name == orderData.item2 then
			item2Found = true
		end

		if item1Found and item2Found then
			break
		end
	end

	if item1Found then
		print("Item 1 found for player: " .. player.Name)

		nearestBox1 = findNearestDeliveryArea(playerPosition)
		if nearestBox1 then
			local tool1 = player.Backpack:FindFirstChild(orderData.item1)
			if tool1 then
				local tool1Clone = cloneTool(tool1, nearestBox1)
				nearestBox1.Transparency = 1
				tool1:Destroy()

				print("Tool 1 cloned and removed from player's inventory")
				resetDeliveryAreaTransparency(tool1Clone, nearestBox1)
			else
				print("No tool 1 found in player's inventory")
			end
		else
			print("No delivery area found near the player for item 1")
		end
	end

	if item2Found then
		print("Item 2 found for player: " .. player.Name)

		nearestBox2 = nil
		for _, box in ipairs(deliveryAreasFolder:GetChildren()) do
			if box ~= nearestBox1 then
				nearestBox2 = box
				break
			end
		end

		if nearestBox2 then
			local tool2 = player.Backpack:FindFirstChild(orderData.item2)
			if tool2 then
				local tool2Clone = cloneTool(tool2, nearestBox2)
				nearestBox2.Transparency = 1
				tool2:Destroy()

				print("Tool 2 cloned and removed from player's inventory")
				resetDeliveryAreaTransparency(tool2Clone, nearestBox2)
			else
				print("No tool 2 found in player's inventory")
			end
		else
			print("No delivery area found near the player for item 2")
		end
	end

	print("Order received by: " .. player.Name)
	
	-- Increment the value of CB in PlayerData by orderData.totalprice
	local playerData = player.PlayerData
	if playerData then
		local CBValue = playerData:FindFirstChild("CB").Value
		if CBValue then
			CBValue = CBValue + orderData.totalprice
			print("CB increased by " .. orderData.totalprice .. " for player: " .. player.Name)
		else
			print("CB value not found in PlayerData for player: " .. player.Name)
		end
	else
		print("PlayerData folder not found for player: " .. player.Name)
	end

	return true, "Order complete by the server side"
end

Oh, forget it! I was able to solve it lol

I’ve changed player.PlayerData to player:FindFirstChild("PlayerData") to ensure we’re correctly locating the PlayerData folder

-- Increment the value of CB in PlayerData by orderData.totalprice
	local playerData = player:FindFirstChild("PlayerData")
	if playerData then
		local CBValue = playerData:FindFirstChild("CB")
		if CBValue then
			CBValue.Value = CBValue.Value + orderData.totalprice
			print("CB increased by " .. orderData.totalprice .. " for player: " .. player.Name)
		else
			print("CB value not found in PlayerData for player: " .. player.Name)
		end
	else
		print("PlayerData folder not found for player: " .. player.Name)
	end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.