Script doesn't appear to do anything at all

I’ve recently been working on one of those “Cart Ride Into…” games because my friend told me to make one, however, I can’t get this script to do anything. It is supposed to spawn a game pass cart (if you have the gamepass) and prompt you to buy it if you don’t own it.

Here is my current script, please help out if you can:

local replicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local objectToClone = replicatedStorage:WaitForChild("chicken roni cart")
local gamePassID = 799594632

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local hasGamePass = false

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

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

		if hasGamePass then
			script.Parent.Click.Triggered:Connect(function(player)
				local clonedObject = objectToClone:Clone()
				clonedObject.Parent = game.Workspace
			end)
		else
			MarketplaceService:PromptGamePassPurchase(player, gamePassID)
		end
	end)
end)

Thank you for your support!

When a player joins, Player.CharacterAdded isn’t fired until the player dies because the character has already been added by then, thats what always seems to happen to me

So is this player.CharacterAdded line causing the whole thing not to work?
If so, how can I fix it?

local replicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local objectToClone = replicatedStorage:WaitForChild("chicken roni cart")
local gamePassID = 799594632

local function checkForGamepass(character)
local hasGamePass = false

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

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

		if hasGamePass then
			script.Parent.Click.Triggered:Connect(function(player)
				local clonedObject = objectToClone:Clone()
				clonedObject.Parent = game.Workspace
			end)
		else
			MarketplaceService:PromptGamePassPurchase(player, gamePassID)
		end
end

game:GetService("Players").PlayerAdded:Connect(function(player)
        if player.Character then
                 checkForGamepass(player.Character)
        end
	player.CharacterAdded:Connect(function(character)
		checkForGamepass(character)
	end)
end)

I didnt write this in Studio so there may be some problems, please double check
basically all i did was put that whole thing in a function outside and everytime a player joins it runs the function and when the player’s character is added it runs the function again

I’ve made some changes but it just doesn’t work for some reason.

local replicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local objectToClone = replicatedStorage:WaitForChild("chicken roni cart")
local gamePassID = 799594632

local function checkForGamepass(player)
	local hasGamePass = false

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

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

	if hasGamePass then
		script.Parent.Parent.Click.Triggered:Connect(function()
			local clonedObject = objectToClone:Clone()
			clonedObject.Parent = game.Workspace
			script.Parent.Parent.BrickColor = BrickColor.new("Bright red")
			wait(6)
			script.Parent.Parent.BrickColor = BrickColor.new("Dark green")
		end)
	else
		MarketplaceService:PromptGamePassPurchase(player, gamePassID)
	end
end

game:GetService("Players").PlayerAdded:Connect(function(player)
	if player.Character then
		checkForGamepass(player)
	end
	player.CharacterAdded:Connect(function(character)
		checkForGamepass(player)
	end)
end)

(also if you are wondering about the changing the color it is because the physical button needs to switch colors when clicked)

Are you getting any errors or warnings in the output?

There’s one that happens at the very beginning, and no other errors occur.
"Triggered is not a valid member of ClickDetector “Workspace.chicken roni cart.chicken roni cart ($4).Head.Click”

I have noticed these few mistakes you did in your script

  1. Event Connection: You’re connecting the Click event of script.Parent.Click inside the PlayerAdded event. This means that every time a player joins, you’re attempting to connect this event again, which can lead to multiple connections and unexpected behavior. Instead, you should connect it outside the PlayerAdded event.
  2. Triggered Event: Inside the Click event, you’re connecting script.Parent.Click.Triggered to a function that expects a player parameter, but the Triggered event doesn’t pass any parameters. You should remove the player parameter from the function inside the Triggered event.
  3. Parent of Script: Make sure that script.Parent refers to the correct object. If you’re intending for it to be the object that the script is a child of, that’s fine.

Now your script with these changes applied to it (Make sure to see if I wrote it right, I didn’t do it in Roblox Studio)

local replicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local objectToClone = replicatedStorage:WaitForChild("chicken roni cart")
local gamePassID = 799594632

script.Parent.Click.Triggered:Connect(function()
    local function spawnCart(player)
        local clonedObject = objectToClone:Clone()
        clonedObject.Parent = game.Workspace
    end

    local function checkGamePass(player)
        local success, message = pcall(function()
            local hasGamePass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
            if hasGamePass then
                spawnCart(player)
            else
                MarketplaceService:PromptGamePassPurchase(player, gamePassID)
            end
        end)
        
        if not success then
            warn("Error while checking if player has gamepass: " .. tostring(message))
        end
    end

    local player = game.Players.LocalPlayer
    if player then
        checkGamePass(player)
    end
end)

It’s giving off this error in output.
"Click is not a valid member of ClickDetector “Workspace.chicken roni cart.chicken roni cart ($4).Head.Click”

It seems that I was trying to connect the Triggered event of a ClickDetector component attached to the “chicken roni cart” model. However, ClickDetector objects do not have a Click event; they only have a MouseClick event.

local replicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local objectToClone = replicatedStorage:WaitForChild("chicken roni cart")
local gamePassID = 799594632

-- Connect the MouseClick event of the ClickDetector
objectToClone.ClickDetector.MouseClick:Connect(function()
    -- Function to handle spawning the cart
    local function spawnCart(player)
        local clonedObject = objectToClone:Clone()
        clonedObject.Parent = game.Workspace
    end

    -- Function to check if the player has the game pass
    local function checkGamePass(player)
        local success, message = pcall(function()
            local hasGamePass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
            if hasGamePass then
                spawnCart(player)
            else
                MarketplaceService:PromptGamePassPurchase(player, gamePassID)
            end
        end)
        
        if not success then
            warn("Error while checking if player has gamepass: " .. tostring(message))
        end
    end

    -- Call checkGamePass for the local player
    local player = game.Players.LocalPlayer
    if player then
        checkGamePass(player)
    end
end)

nope, this is just not true, players.PlayerAdded fires before the first ever player.CharacterAdded, because the player object loads before a character, that’s why when you load in, you can sometimes not see your character, and after a few seconds your character becomes visible.

there is probably something wrong with op’s studio because I tested op’s script and it works, I used op’s pass at first, it prompted it, I then decided to use one of the passes I own, and test it again, and when I walk over the part it prints “ran”.

it works for me, so there might be some other script or logic that is interfering with the script. because this script itself works.

hope this helps!
edit: nvm I just read the msg where he shared his output, the problem is totally using the wrong event name…

I fixed the error popping up, but nothing happens. There’s no error, but nothing happens.
Here is the code.

local replicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local objectToClone = replicatedStorage:WaitForChild("chicken roni cart")
local gamePassID = 799594632

script.Parent.Parent.Click.MouseClick:Connect(function()
	local function spawnCart(player)
		local clonedObject = objectToClone:Clone()
		clonedObject.Parent = game.Workspace
	end

	local function checkGamePass(player)
		local success, message = pcall(function()
			local hasGamePass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
			if hasGamePass then
				spawnCart(player)
			else
				MarketplaceService:PromptGamePassPurchase(player, gamePassID)
			end
		end)

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

	local player = game.Players.LocalPlayer
	if player then
		checkGamePass(player)
	end
end)

I have fixed it now, please don’t respond anymore!


local replicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local objectToClone = replicatedStorage:WaitForChild("chicken roni cart")
local gamePassID = 799594632

script.Parent.Parent.ClickDetector.MouseClick:Connect(function(player)
	local function spawnCart()
		local clonedObject = objectToClone:Clone()
		clonedObject.Parent = game.Workspace
	end

	local function checkGamePass()
		local success, message = pcall(function()
			local hasGamePass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
			if hasGamePass then
				spawnCart()
			else
				MarketplaceService:PromptGamePassPurchase(player, gamePassID)
			end
		end)

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

	checkGamePass()
end)

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