I need help with my gamepass script

  1. What do you want to achieve? Keep it simple and clear!

so basically I am trying to make a script that when you click on the spawn location it will prompt gamepass is being wrapped in pcall to check if the purchase was successful and if the player owns the gamepass and they click on the spawnlocation again it will give them a random tool from the folder then if they leave it will save their tool so the next time the player joins it will load the previous tool into their backpack.

  1. What is the issue? Include screenshots / videos if possible!

I am not a very good scripter and I was struggling a lot while scripting this thats why I came here for help.

the problem is when I click on the spawn location it gives me two error
:arrow_forward: Unable to cast value to Object
ServerScriptService.Script:40: attempt to index nil with 'Value’

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried looking for solution to my problem on devforum, youtube, google but cant find a way to fix it.

local datastore = game:GetService("DataStoreService"):GetDataStore("Tools")
local MPS = game:GetService("MarketplaceService")
local CD = Instance.new("ClickDetector", workspace.SpawnLocation)
local pass_id = 10883698

game.Players.PlayerAdded:Connect(function(player)
	local Owned_Tools = Instance.new("Folder", player)
	local Owned = Instance.new("BoolValue")
	
	Owned_Tools.Name = "Inventory"
	Owned.Name = "G"
	Owned.Value = false
	
	CD.MouseClick:Connect(function()
		local success, err = pcall(function()
			MPS:PromptGamePassPurchase(player.UserId, pass_id)
		end)
		if success then
			print(player.Name .. " successfully bought the gamepass")
			Owned = true
			
			local Tools = game.ReplicatedStorage.Random_Tools:GetChildren()
			local Randomizer = Tools[math.random(1, #Tools)]
			
			print(player.Name .. " got " .. Randomizer.Name)
			local suc, erro = pcall(function()
				local p1
				p1 = datastore:GetAsync(player.UserId, Randomizer.Name)
			end)
			if suc then
				print("Player data has been loaded!")
			end
		elseif not success then
			print(err)
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local p2 = player.Inventory:FindFirstChild("G").Value
	local s,e = pcall(function()
		datastore:SetAsync(player.UserId, p2)
	end)
	if s then
		print("Player data has been successfully saved!")
	end
end)

In the middle of the script, you said Owned = true instead of Owned.Value = true.

2 Likes

mb I think I need a spectacle lol

thanks for correcting me tho :slight_smile:

If his reply helped, please mark it as the solution so people know it’s solved.

You forgot to parent Owned to the Inventory.

	Owned.Name = "G"
    Owned.Parent = Owned_Tools
	Owned.Value = false

or

	local Owned = Instance.new("BoolValue", Owned_Tools)
1 Like

Remove the second part, it is not recommended and not effiicient.
The correct way is :
1.Instancing
2.Properties
3.Parenting.

Thus, I’d replace these:

local Owned_Tools = Instance.new("Folder", player)
	local Owned = Instance.new("BoolValue")
	
	Owned_Tools.Name = "Inventory"
	Owned.Name = "G"
	Owned.Value = false

in this:

local Owned_Tools = Instance.new("Folder")
local Owned = Instance.new("BoolValue")
	
Owned_Tools.Name = "Inventory"
Owned_Tools.Parent = player

Owned.Name = "G"
Owned.Value = false
Owned.Parent =  Owned_Tools

I added it to keep it consistent with their previous code.

1 Like