- 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.
- 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
Unable to cast value to Object
ServerScriptService.Script:40: attempt to index nil with 'Value’
- 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)
