Height reroll product's script is working when purchased

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

  1. What do you want to achieve?
    I want to create a system in my Roblox game where players can purchase a dev product that allows them to reroll their character’s height using BodyHeightScale. When purchased, The new height should be randomly selected within a specified range and saved to the player’s data. The player’s character should automatically respawn with the new height applied.
  2. What is the issue?
    after purchasing the product, the player’s height is not changing, the data is not being saved, and the character is not respawning automatically. There are no errors in the output making it harder for me to understand the problem.
  3. What solutions have you tried so far?
  • Checking that the humanoid.Health = 0 line is included to kill the player’s character and trigger a respawn, but that didn’t even do anything because my character never died when purchasing the product

This is the code when its purchased:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local heightStore = DataStoreService:GetDataStore("PlayerHeights")

local ProductId = 1809068110 

local function setHeightAndRespawn(player)
	-- height reroll
	local newHeight = math.random(110, 150) / 100
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.BodyHeightScale.Value = newHeight
	-- saving new height to datastore
	pcall(function()
		heightStore:SetAsync(tostring(player.UserId), newHeight)
	end)
	-- respawn
	humanoid.Health = 0
end
	-- product purchase handler 
local function onProductPurchase(playerId, receiptInfo)
	-- double checking if the receipt is valid and for the correct product
	if receiptInfo and receiptInfo.ProductId == ProductId then
		local player = Players:GetPlayerByUserId(playerId)
		if player then
			setHeightAndRespawn(player)
		end
		return Enum.ProductPurchaseDecision.PurchaseGranted
	else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
end

MarketplaceService.ProcessReceipt = onProductPurchase

I have no clue what could be missing or wrong with this script and also this is my first topic!

debug with print() and/or breakpoints to ensure everything is running. It is very hard for anyone on here to tell what the specific issue is. Will be much easier for you to pinpoint that issue, then ask about it.

In other words, when you find out which line is stopping your code I will be able to help you.

after debugging and looking, the only time it printed was when it said “Line 27”

local function setHeightAndRespawn(player)
	-- height reroll
	local newHeight = math.random(110, 150) / 100
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.BodyHeightScale.Value = newHeight
	print("New height set:", newHeight) -- Check if newHeight is correctly calculated
	-- saving new height to datastore
	pcall(function()
		heightStore:SetAsync(tostring(player.UserId), newHeight)
		print("Height saved to datastore for player:", player.Name) -- Verify if height is saved
	end)
	-- respawn
	humanoid.Health = 0
	print("Player respawned:", player.Name) -- Verify if player is respawned
end
-- product purchase handler
local function onProductPurchase(playerId, receiptInfo)
	print("Line 27")
	-- double checking if the receipt is valid and for the correct product
	if receiptInfo and receiptInfo.ProductId == ProductId then
		local player = Players:GetPlayerByUserId(playerId)
		if player then
			setHeightAndRespawn(player)
		end
		return Enum.ProductPurchaseDecision.PurchaseGranted
	else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
end

MarketplaceService.ProcessReceipt = onProductPurchase

running
Before purchasing
image
After purchasing
image