Issues With Spawning Multiple Parts When Gamepass is Purchased

Hello, so I am making this little game where every click spawns a part, and I have a gamepass that spawns 4 parts instead of one. The issue is that it is still spawning one, even though it should be spawning 4.

I have looked on DevForums but didn’t really find any useful information, but I do suspect that it is an issue with the for loops or the gamepass not getting detected, but I am not too sure.

Here is my script:

local event = game.ReplicatedStorage:WaitForChild("CreatePartEvent")
local MarketplaceService = game:GetService("MarketplaceService")
local passId = 983247902

game.Players.PlayerAdded:Connect(function(player)
	event.OnServerEvent:Connect(function(player, hitPosition)
		local hasGamepass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, passId)

		if hasGamepass then
			for i = 1, 4 do
				local newPart = Instance.new("Part")
				newPart.Position = hitPosition
				newPart.Parent = workspace.PartsFolder
				newPart.Name = "newPart"
			end
		else
			local newPart = Instance.new("Part")
			newPart.Position = hitPosition
			newPart.Parent = workspace.PartsFolder
			newPart.Name = "newPart"
		end
	end)
end)

1 Like

u dont need to check playeradded, as every onserverevent’s first argument is always player so try

local event = game.ReplicatedStorage:WaitForChild("CreatePartEvent")
local MarketplaceService = game:GetService("MarketplaceService")
local passId = 983247902

event.OnServerEvent:Connect(function(player, hitPosition)
	local hasGamepass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, passId)
	if hasGamepass then
		for i = 1, 4 do
			local newPart = Instance.new("Part")
			newPart.Position = hitPosition
			newPart.Parent = workspace.PartsFolder
			newPart.Name = "newPart"
		end
	else
		local newPart = Instance.new("Part")
		newPart.Position = hitPosition
		newPart.Parent = workspace.PartsFolder
		newPart.Name = "newPart"
	end
end)
1 Like

Thanks for the reply, it is still spawing only one part, instead of four

What do you think is causing the issue

Try hasGamepass == true. Maybe that will help?

Still doesn’t work, and still spawns one part

Try adding print statements for variables and see if everything seems to look good. Prints for the Player variable, hasGamepass variable, ect. See if anything looks out of place.

It seems that roblox isn’t detecting that the gamepass has been purchased. I added a print statement to when the player doesn’t have the gamepass, and wants to spawn a part, and added a different print statement to when the player wants to spawn a part, and has a gamepass, and it printed the first one even when the gamepass has been purchased.

It just clicked to me. It might be one of those situations where the game wont know when you buy a gamepass until usually you join another server. Try testing it outside of studio and just running the game normally. You should own the gamepass if you created it and it should work.

Hi, thanks, it worked, I didn’t know that was the issue.

set their reply as a solution so those who have this problem can solve it

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