Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[UNANSWERED]Why isn't this scripting working correctly!?

I keep getting the error code: Unable to cast the value to Object I do not see anything wrong, please help. My code I am using is:

--andorks
script.Parent.MouseButton1Down:connect(function(player)
	game:GetService("MarketplaceService"):PromptPurchase(player, 16630147)
end)

2 answers

✓ Accepted answer

This is a bit tricky, considering the parent of the script, but the steps are fairly basic. Step 1: Move the entire SurfaceGui (not the brick, though) into StarterGui. Then, select the SurfaceGui, hold Ctrl, and select the brick it was in before. Without deselecting either item, type this in the command bar: local s=game.Selection:Get() s[1].Adornee=s[2] This should allow the SurfaceGui to function from StarterGui. Now for the main code. Type this in a regular script inside of the button.

local player=script.Parent.Parent.Parent.Parent --Order of parents: button, SurfaceGui, PlayerGui, player
local id=0 --insert ID of asset here
function click() --initiate our function
game:GetService("MarketplaceService"):PromptPurchase(player,id) --don8 pl0x
end --end our function
script.Parent.MouseButton1Click:connect(click) --connect the function to the MouseButton1Click event

That should do it! Contact me with any questions.

It's because of your "player" argument. MouseButton1Down doesn't return the Player who clicks, it return x/y coordinates. Instead, remove the argument in line 2: player.

Now, change the player argument in line 3 into this: game.Players.LocalPlayer MAKE SURE IT'S A LOCAL SCRIPT, IF IT'S NOT, MAKE IT ONE

Your finished script will look like this:

--andorks
script.Parent.MouseButton1Down:connect(function()
    game:GetService("MarketplaceService"):PromptPurchase(game.Players.LocalPlayer, 16630147)
end)

9 comments

0

Thanks!

0

Now it doesn't work..

0

'PromptPurchase' does not work within a LocalScript; It is Server-Sided only sadly.

0

So, I should put it in a script?

0

You could, but, that will take a lot more editing of the code. :/

0

So, I have to edit it, or it won't work :/

0

Yep, pretty much. :/ You would have to edit the code so that it knows which target (the Player) is (As those repeatedly use the script.Parent.Parent until it reaches the Player).

0

Sad thing is, it's in a surfacegui

0

Helping him in ROBLOX PM's.

This discussion is preserved as it was. New answers, comments, and votes are closed.