Intermittent Script Error

Hello! I’ve made a shop script that pulls values from replicated storage, the values are setup like so image This is the error players have received

Thats all i have at the moment to go off, I cannot replicate this in studio nor game. These are the functions where I believe the error is occurring

function msg(t, v, l)
	local board = script.Parent.Main.msg
	if t == "c" then
		board.sub.Text = "Confirmation"
		board.msg.Text = ("Are you sure you want to buy the "..l.." for "..v)
		board.ack.Visible = false
		board.confirm.Visible = true
		board.cancel.Visible = true
		board.Visible = true
		main.vehinfo.Visible = false
		main.Sidebar.Visible = false
		board.cancel.MouseButton1Click:Connect(function()
			board.Visible = false
			main.vehinfo.Visible = true
			main.Sidebar.Visible = true
			print("declined")
		end)
		board.confirm.MouseButton1Click:Connect(function()
			sendpurchaserequest(currentcar)
			print(currentcar.."thisss")
			board.Visible = false
			
			main.vehinfo.Visible = true
			main.Sidebar.Visible = true
		end)
	end
	if t == "w" then
		board.ack.Visible = true
		board.confirm.Visible = false
		board.cancel.Visible = false
		board.Visible = true
		board.sub.Text = "Error"
		board.msg.Text = ("This vehicle requires the "..v.." gamepass!")
		Marketplace:PromptGamePassPurchase(plr.UserId,l)
		main.vehinfo.Visible = false
		main.Sidebar.Visible = false
		board.ack.MouseButton1Click:Connect(function()
			board.Visible = false
			main.vehinfo.Visible = true
			main.Sidebar.Visible = true
		end)
	end
	if t == "i" then
		board.ack.Visible = true
		board.confirm.Visible = false
		board.cancel.Visible = false
		board.Visible = true
		board.sub.Text = "Error"
		board.msg.Text = ("Insuffeicent Funds!")
		main.vehinfo.Visible = false
		main.Sidebar.Visible = false
		board.ack.MouseButton1Click:Connect(function()
			board.Visible = false
			main.vehinfo.Visible = true
			main.Sidebar.Visible = true
		end)
	end
	if t == "a" then
		board.ack.Visible = true
		board.confirm.Visible = false
		board.cancel.Visible = false
		board.Visible = true
		board.sub.Text = v
		board.msg.Text = l
		main.vehinfo.Visible = false
		main.Sidebar.Visible = false
		board.ack.MouseButton1Click:Connect(function()
			board.Visible = false
			main.vehinfo.Visible = true
			main.Sidebar.Visible = true
		end)
	end
end
--checkpurchasability
function buy(v)
print(v.."buylevel")
local outcome = false
local stats = assets:WaitForChild(currentcar)
	if stats.isagamepass.Value == true then
		if Marketplace:UserOwnsGamePassAsync(plr.UserId, stats.gamepassid.Value) then
			if maing.plrmonraw.Value >= stats.rawprice.Value then
			msg("c",stats.prettyprice.Value,currentcar)
			else
			msg("i")	
			end
		else
			msg("w",stats.gamepassname.Value,stats.gamepassid.Value)
		end
	else
		if maing.plrmonraw.Value >= stats.rawprice.Value then
			msg("c",stats.prettyprice.Value,currentcar)
			else
			msg("i")	
			end
	end
end

What Line is has the Error?

30chars

Not sure, that’s all I have to go off.

It would be line 276

30 chars…

oh oops, I thought it stated the line in that first statement, this is line 276 buy(currentcar)

It says 276 is calling the function that calls the function that errors at line 62. The actual error is at 62.

@JoneTech The error is because you’re calling PromptGamePassPurchase with the player’s UserId, but it’s supposed to take the player instance itself as an argument. Just change this line:

To this:

Marketplace:PromptGamePassPurchase(plr,l)

3 Likes

Okay, I’ll try that, but any reason why it works for some and doesn’t for others? Or is it just weird like that?

t probably isn’t equal to “w” for most users, so it won’t ever get to that line.

2 Likes

Oh, and one more thing I forgot to add, when the error does occur, I can change the NumberValue containing the passid to 0 and back and it magically works, I dunno if that changes anything.

That’s weird, but the change I suggested should fix it regardless.

2 Likes

Okay, Im getting 13:27:42.041 - Unable to cast Instance to int64 , on this line if Marketplace:UserOwnsGamePassAsync(plr, stats.gamepassid.Value) then

You didn’t need to change that part to use the player, it can keep using the UserId. Prompting a purchase requires the Player; it requires the user to actually be in-game, since, well, they need to be there to press buy. On the other hand, you can always check if a user owns something using their UserId, rather than needing their actual Player object ingame. It’s a bit inconsistent I guess, but it makes sense if you think about it.

2 Likes

This is all in a localscript, so plr is the local player, but it was doing this before I added the PromptPurchase. :thinking:

Yeah, I’m saying you could use the original code where you check UserOwnsGamePassAsync with plr.UserId, it’s just PromptGamePassPurchase that needs to use the plr object itself.

2 Likes