Unable to cast value to Object when using Remote Events (Server -> Client)

Hi! :slight_smile:

I’m making a Purchase System for an Obby. I will try my best to explain this!

When someone clicks the Buy! button in the shop, to buy a DevProduct, this LocalScript executes (it is inside the button), and it works:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local devproductPromptEvent = ReplicatedStorage:WaitForChild("DevProductPromptEvent")
local button = script.Parent
local loading = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent:WaitForChild("Loading").Frame

button.MouseButton1Down:Connect(function()
	devproductPromptEvent:FireServer(*DevProductHere*)
	loading.Visible = true
	print("DevProduct Prompt Event Fired!")
end)

That Remote Event then fires this normal script that is inside ServerScriptService and everything works too:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local gamepassPromptEvent = ReplicatedStorage.GamepassPromptEvent --For gamepasses
local devproductPromptEvent = ReplicatedStorage.DevProductPromptEvent --For devproducts

--This only executes if the last script is selling a gamepass and not a devproduct
gamepassPromptEvent.OnServerEvent:Connect(function(player, id)
	MarketplaceService:PromptGamePassPurchase(player, id)
	print("Gamepass Prompted!")
end)
--End

--This Remote Event is the one that the last script fired
devproductPromptEvent.OnServerEvent:Connect(function(player, id)
	MarketplaceService:PromptProductPurchase(player, id)
	print("DevProduct Prompted!")
end)

Then, that DevProductPromptEvent fires this script in line 17 because the player was trying to purchase a DevProduct (not a Gamepass) and it goes to line 22 because the player canceled the purchase, but then the Remote Event in that line isn’t fired…

local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local purchaseFinishedEvent = ReplicatedStorage:WaitForChild("PurchaseFinishedEvent")
local purchaseUnfinishedEvent = ReplicatedStorage:WaitForChild("PurchaseUnfinishedEvent")

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, assetId, isPurchased)
	if isPurchased then
		purchaseFinishedEvent:FireClient(player) --*This doesn't work!!!*
		print(player.Name .. " bought an item with AssetID: " .. assetId)
	else
		purchaseUnfinishedEvent:FireClient(player) --*This doesn't work!!!*
		print(player.Name .. " didn't buy an item with AssetID: " .. assetId)
	end
end)

MarketplaceService.PromptProductPurchaseFinished:Connect(function(player, assetId, isPurchased) --Goes to here because the player was buying a DevProduct
	if isPurchased then
		purchaseFinishedEvent:FireClient(player) --*This doesn't work!!!*
		print(player.Name .. " bought an item with AssetID: " .. assetId)
	else
		purchaseUnfinishedEvent:FireClient(player) --*Tries to fire this Remote Event and it doesn't work!!!*
		print(player.Name .. " didn't buy an item with AssetID: " .. assetId)
	end
end)

Everything in this script works, except the part that then tries to fire the LocalScript that is in StarterPlayerScripts because of this error in line 22, but this will also happen in line 9, 12 and 19:

a

I know that this is very confusing, but can someone help me fix this issue, editing the script?

Thanks! <3

try this :

:FireClient(game.Players[player])

if this doesnt work too

:FireClient(game.Players[player.Name])
1 Like

Thanks for the fast answer!

In the last script (the one that had the error) I changed it to the first option and it gave me this error in line 22:

164852011 is not a valid member of Players "Players"

And the second option:

ServerScriptService.TYMessage:22: attempt to index number with 'Name'

what is your product id ?

Is Player a NumberValue, intvalue, etc?..

try this

:FireClient(game.Players:GetPlayerByUserId(player))
2 Likes

Gave this error at line 23 but I think it fired the Remote Event because I got an error from the LocalScript found in StarterPlayerScripts:

ServerScriptService.TYMessage:23: attempt to index number with 'Name'

yes, this means that it works, do the same thing with the print line too, mark as solution if helped,have a good day!

Thank you! Have a nice day too! :smiley:

like this :

print(game.Players:GetPlayerByUserId(player).Name.."You text here")
1 Like