Part.touched not working / gamepass function

This is my entire script. When I touch the block, nothing happens. It should print “You already have the pass”. I know it works because a separate player added event tells me that I own the pass. How can I fix this? script.Parent is a part. This is in a local script.

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

local gamePassID = 26544839  -- Change this to your game pass ID

-- Function to prompt purchase of the game pass
local function promptPurchase()

	local player = Players.LocalPlayer
	local hasPass = false

	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)

	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end

	if hasPass then
		-- Player already owns the game pass; tell them somehow
		print("You already have the pass")
	else
		-- Player does NOT own the game pass; prompt them to purchase
		MarketplaceService:PromptGamePassPurchase(player, gamePassID)
	end
end

script.Parent.Touched:Connect(promptPurchase)

Where is your LocalScript located?

1 Like

It is the child of a part whose parent is the workspace

LocalScripts don’t work in the workspace.
You will have to put it in StarterGui and change the line to

game.Workspace.YourPart.Touched:Connect(promptPurchase)
1 Like

Workspace is the server. LocalScripts only work locally/on the client to each player individually. StarterPlayerScripts, StarterGui or ReplicatedFirst. LocalScripts will run in these areas. From here you can also clone other LocalScripts into Workspace & that will work too, because it’s not made by the server.

1 Like