Hi there!
I’m trying to add pets to my game and I need a system that checks if the player can equip a pet and if they can equips it. I hit a wall in my coding.
Problem:
Using
DataStore2("Pets", plr):Get({})in scripts returns and empty table,{}, even if I set it to {“Pet”}This doesnt occur when I try to
:Get()a data store from the console.
Heres my script (part of a module):
local module = {}
local random = math.random
local DS2 = require(game:GetService("ServerScriptService").Data.DataStore2)
local Notify = require(game:GetService("ServerScriptService").Notifications).Notify
local ExisitingPets = {"Weak Brick", "Rob", "Mechanic", "Bob", "Goat", "Bawb"}
-- Equip Pets --
function canEquipPetAgain(plr: Player, Pet, Pets, EquippedPets)
print(Pets:Get()) -- This returns {}
print(EquippedPets:Get()) -- This returns {}
local MaxEquipablePets = 3
-- --
local FoundPetsInventory = 0
for _, LoopPet in pairs(Pets:Get({})) do
if LoopPet == Pet then
FoundPetsInventory += 1
end
end
local FoundPetsEquipped = 0
for _, LoopPet in pairs(EquippedPets:Get({})) do
if LoopPet == Pet then
FoundPetsEquipped += 1
end
end
-- Correcting --
if FoundPetsEquipped > FoundPetsInventory or #EquippedPets:Get({}) > MaxEquipablePets then
Notify(plr, "The game noticed an error and corrected it.\nYour pets have been unequipped due to an error.", Color3.new(1, 0, 0))
end
-- Returning --
if FoundPetsEquipped+1 > FoundPetsInventory then
print(FoundPetsEquipped+1 .. " ".. FoundPetsInventory)
Notify(plr, "You already equipped this pet.", Color3.new(1, 0, 0))
return false
elseif #EquippedPets:Get({})+1 > MaxEquipablePets then
Notify(plr, "You already equipped " .. #EquippedPets:Get({}) / MaxEquipablePets .. " Pets.", Color3.new(1, 0, 0))
return false
elseif FoundPetsEquipped+1 <= FoundPetsInventory then
return true
else
Notify(plr, "Something went wrong. (NRCEPAF)", Color3.new(1, 0, 0))
return false
end
end
function module.EquipPet(plr:Player, Pet:string)
local Pets = DS2("Pets", plr)
local EquippedPets = DS2("EquippedPets", plr)
-- --
--[[if Pets:Get({}) == nil or #Pets:Get({}) == 0 or not table.find(ExisitingPets, Pet) or not table.find(Pets:Get({}), Pet) then
Notify(plr, "Something went wrong (NPEP)", Color3.new(1, 0, 0))
return
end]]
-- --
if canEquipPetAgain(plr, Pet, Pets, EquippedPets) == false then
return
end
-- --
if EquippedPets:Get({}) == nil or #EquippedPets:Get({}) == 0 and ExisitingPets[Pet] then
EquippedPets:Set({Pet})
return
elseif EquippedPets:Get({}) ~= nil and #EquippedPets:Get({}) ~= 0 and ExisitingPets[Pet] then
local oldTable = EquippedPets:Get({})
oldTable[#oldTable+1] = Pet
local newTable = oldTable
EquippedPets:Set(newTable)
return
end
end
Thanks for you help in advance ![]()