What do you want to achieve? I want to make it give a better sword if a player owns the gamepass and if they don’t, they get the normal sword. I don’t want players to get multiple swords.
local marketplaceService = game:GetService('MarketplaceService')
local gamepassID = 40443369
script.Parent.Touched:Connect(function(Hit)
print("Sword Detector: Touched")
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
local ClassicSword = Player.Backpack:FindFirstChild("ClassicSword")
local HighSword = Player.Backpack:FindFirstChild("HighSword")
if not ClassicSword then
local ClassicSword = game.ServerStorage.ClassicSword:Clone()
ClassicSword.Parent = Player.Backpack
end
if marketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepassID) then
if ClassicSword then
Player.Backpack.ClassicSword:Destroy()
end
if not HighSword then
local Sword = game.ServerStorage.HighSword:Clone()
Sword.Parent = Player.Backpack
end
print("Sword Detector: User owns pass")
end
end)
if ClassicSword then
Player.Backpack.ClassicSword:Destroy()
end
You’re removing a nil value, for you search for it beforehand.
The following script should fix your issue:
local marketplaceService = game:GetService('MarketplaceService')
local gamepassID = 40443369
script.Parent.Touched:Connect(function(Hit)
print("Sword Detector: Touched")
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
local ClassicSword = Player.Backpack:FindFirstChild("ClassicSword")
local HighSword = Player.Backpack:FindFirstChild("HighSword")
if marketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepassID) then
if ClassicSword then
game:GetService("Debris"):AddItem(ClassicSword, 0.01)
end
if not HighSword then
game.ServerStorage.HighSword:Clone().Parent = Player.Backpack
end
print("Sword Detector: User owns pass")
elseif not ClassicSword then
game.ServerStorage.ClassicSword:Clone().Parent = Player.Backpack
end
end)
I’m on my phone as of now, so I quickly threw this together:
local marketplaceService = game:GetService('MarketplaceService')
local gamepassID = 40443369
local dbPlayers = {}
local db = 0
function Claim(val, parent)
table.insert(dbPlayers, parent)
if val then
game.ServerStorage.HighSword:Clone().Parent = parent.Backpack
else
game.ServerStorage.ClassicSword:Clone().Parent = parent.Backpack
end
end
script.Parent.Touched:Connect(function(Hit)
if os.clock() - db >= .1 then
db = os.clock();
print("Sword Detector: Touched")
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
if Player then
local claimed = false;
for i, v in pairs(dbPlayers) do
if v == Player then
claimed = true;
break;
end
end
if not claimed then
if marketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepassID) then
print("Sword Detector: User owns pass")
spawn(function() Claim(true, Player) end)
else
spawn(function() Claim(false, Player) end)
end
end
end
end
end)