Unable to cast value to Object

Greetings,

I am getting this error and I do not know what I have mistaken, please help!

The error:

Screenshot 2022-11-27 005745

Script:

local plr2 = game:GetService("Players").LocalPlayer
local Player = game:GetService("Players").LocalPlayer.UserId
local gamePassID = 1340906699

button.MouseButton1Click:Connect(function()

MarketplaceService:PromptPurchase(Player, gamePassID) -- Line 13

Purchase:FireServer(plr2)

end)
1 Like

you have player defined as LocalPlayer.UserId, Which is a value, Remove the .UserId part of it and then it should work

1 Like

Can you show your Event?, Replace Player with plr2 btw, don’t just completely remove it like what @CQuaral said

I’m not sure if MarketplaceService works in a LocalScript

1 Like

It’s because you’re trying to pass in an integer into an argument of the PromptPurchase method that requires an instance of a Player object. The solution is to just remove the .UserId property from the Player variable. However, since you’ll now have 2 variables both assigned to the player, you can just remove it like so:

local Player = game:GetService("Players").LocalPlayer
local gamePassID = 1340906699

button.MouseButton1Click:Connect(function()
    MarketplaceService:PromptPurchase(Player, gamePassID)
    Purchase:FireServer(Player)
end)

I’m not sure what Purchase is, I’m trusting you have a RemoteEvent variable stored in the script somewhere. :slight_smile:

Also for future reference, you don’t need to re-index .LocalPlayer and get the UserId in a separate variable like that. You should do this from now on:

local Player = game:GetService("Players").LocalPlayer
local plr2 = Player.UserId
1 Like

This was already covered by @CQuaral

1 Like

I know, I was writing this solution before it was covered by them. :slight_smile:

Thank you everyone, now it works very nicely!

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