Tool not giving to a player when they enter?

so i want to have a shop where they can buy the product and where if they get off the game then get back on they still have the tool, right? well i have code to check for when a player joins and give them the tool, but apparently it isn’t working.
code:

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local ServerStorage = game:GetService("ServerStorage")

-- Assuming the BoomBox tool is stored in ServerStorage for cloning purposes
-- Make sure to move the BoomBox tool from Workspace to ServerStorage in your game
local boomboxTool = game.Lighting.Sign

-- Replace GAMEPASS_ID with the actual ID of your Boombox gamepass
local VIP_GAMEPASS_ID = 824824655 -- Example Gamepass ID, replace with your own

-- Function to handle giving the Boombox to a player
local function giveBoombox(player)
	if not boomboxTool then
		warn("Boombox tool not found in ServerStorage")
		return
	end

	-- Check if the player already has a Boombox
	if not player.Backpack:FindFirstChild(boomboxTool.Name) and not player.Character:FindFirstChild(boomboxTool.Name) then
		local clone = boomboxTool:Clone()
		clone.Parent = player.Backpack
	end
end

-- Function to check if the player has the gamepass and give them the Boombox
local function onPlayerAdded(player)
	-- Using pcall to handle any errors that might occur when checking for the gamepass
	local success, hasGamepass = pcall(function()
		return MarketplaceService:UserOwnsGamePassAsync(player.UserId, VIP_GAMEPASS_ID)
	end)

	if success and hasGamepass then
		giveBoombox(player)
	else
		if not success then
			warn("Failed to check if player owns Boombox gamepass")
		end
	end
end

-- Connect the onPlayerAdded function to the PlayerAdded event
Players.PlayerAdded:Connect(onPlayerAdded)

-- Iterate through all existing players in case the script is added while the game is running
for _, player in ipairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end

-- Listen for gamepass purchase
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, gamePassId, wasPurchased)
	if gamePassId == VIP_GAMEPASS_ID and wasPurchased then
		giveBoombox(player)
	end
end)

thank you in advance

so i just put some print staements in the code:

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local ServerStorage = game:GetService("ServerStorage")

-- Assuming the BoomBox tool is stored in ServerStorage for cloning purposes
-- Make sure to move the BoomBox tool from Workspace to ServerStorage in your game
local boomboxTool = game.Lighting.Sign

-- Replace GAMEPASS_ID with the actual ID of your Boombox gamepass
local VIP_GAMEPASS_ID = 824824655 -- Example Gamepass ID, replace with your own

-- Function to handle giving the Boombox to a player
local function giveBoombox(player)
	if not boomboxTool then
		warn("Boombox tool not found in ServerStorage")
		return
	end

	-- Check if the player already has a Boombox
	if not player.Backpack:FindFirstChild(boomboxTool.Name) and not player.Character:FindFirstChild(boomboxTool.Name) then
		print("about to give gamepass")
		local clone = boomboxTool:Clone()
		clone.Parent = player.Backpack
	end
end

-- Function to check if the player has the gamepass and give them the Boombox
local function onPlayerAdded(player)
	print("detected you joined")
	-- Using pcall to handle any errors that might occur when checking for the gamepass
	local success, hasGamepass = pcall(function()
		return MarketplaceService:UserOwnsGamePassAsync(player.UserId, VIP_GAMEPASS_ID)
	end)

	if success and hasGamepass then
		print("says you have gamepass")
		giveBoombox(player)
		print("gamepass given")
	else
		if not success then
			warn("Failed to check if player owns Boombox gamepass")
		end
	end
end

-- Connect the onPlayerAdded function to the PlayerAdded event
Players.PlayerAdded:Connect(onPlayerAdded)

-- Iterate through all existing players in case the script is added while the game is running
for _, player in ipairs(Players:GetPlayers()) do
	onPlayerAdded(player)
	
end

-- Listen for gamepass purchase
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, gamePassId, wasPurchased)
	if gamePassId == VIP_GAMEPASS_ID and wasPurchased then
		giveBoombox(player)
	end
end)

so that’s great but this was my output:


since it didnt even print “detected you joined”, it didn’t even detect that i joined the game?
still don’t know how to fix this butthis is a step in the right direction.
if you have any idea on how to help, please post it! that would be much appreciated!

Can you try to see whether the print statement works in a real Roblox server?
Or you can try restarting studio.

how would i see the output in a real server?
and i will restart it right now.

Have you heard of something called Developer Console?

no I actually have not, that is quite usefull, thank you!

1 Like

so i just published to roblox and played, and i didn’t get the tool. which is wierd because it worked in studio.
these are the logs from ingame:


it says i have the gamepass, but it doesn’t get any farther than that

Try this giveBoombox function instead of the other one. Since it seems to say FindFirstChild is nil, that means the object you are trying to search is most likely nil. Try waiting for the variables to load, then do the detection.

local function giveBoombox(player)
	repeat task.wait() until player ~= nil and player.Backpack ~= nil and player.Character ~= nil
	
	if not boomboxTool then
		warn("Boombox tool not found in ServerStorage")
		return
	end

	-- Check if the player already has a Boombox
	if not player.Backpack:FindFirstChild(boomboxTool.Name) and not player.Character:FindFirstChild(boomboxTool.Name) then
		print("about to give gamepass")
		local clone = boomboxTool:Clone()
		clone.Parent = player.Backpack
	end
end

EDIT: If the script doesn’t continue, check if the player variable in nil. That could be the reason the object is nil.

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