Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why can't I add multiple pets to my pet shop GUI?

Asked by 4 years ago

So in my simulator game I added a pet shop GUI. Everything worked fine until I added a second pet. This is the link to the un-copylocked pet shop (Made by @Azarth) the main script is this:

-- this was for a question on scripting helpers - basic client server logic

local PetStorage = game:GetService('ServerStorage').Pets
local RemoteEvent = game:GetService("ReplicatedStorage").PetGiver
local NotificationModule = require ( script.Notification )
local Pets = {
    ['Cube'] = { Pet = PetStorage:WaitForChild("Cube"), Cost = 0 },
    ['Slime'] = { Pet = PetStorage:WaitForChild("Slime"), Cost = 1 }
}

local PlayerPets = {}

local function Assign(Player, newPet)
    -- Delete old assigned pet to make way for new pet
    if PlayerPets[Player] then 
        PlayerPets[Player]:Destroy()
    end
    -- Get previous pet name or newPet from Pets
    local Pet = newPet and Pets[newPet].Pet:Clone() or Pets[PlayerPets[Player].Name].Pet:Clone()
    -- Give pet
    Pet.Parent = Player.Character
    -- Assign pet
    PlayerPets[Player] = Pet
    -- SetNetworkOwner erorrs if Parent doesn't exist yet
    repeat wait() until Player.Character
    Pet.PrimaryPart:SetNetworkOwner(Player)
end

RemoteEvent.OnServerEvent:Connect(function(PlayerWhoFired, PetName)
    assert(PetName, 'Provide a PetName')
    assert(typeof(PetName) == 'string', 'PetName has to be a string!')
    local MatchingKey = Pets[PetName]
    -- is the pet even real? Client could have lied
    if Pets[PetName] then 
        local leaderstats = PlayerWhoFired:FindFirstChild("leaderstats")
        local rebirths = leaderstats and leaderstats:FindFirstChild("rebirths")
        -- Are our rebirths actually >= to the requirement? 
        if rebirths then 
            if rebirths.Value >= MatchingKey.Cost then 
                -- They are, assign the new pet.
                Assign(PlayerWhoFired, PetName)
            else
                -- Are not, notify client they lost the battle
                NotificationModule.Message(PlayerWhoFired, ("You don't have enough"):format() )
            end     
        end
    else
        -- Pet isn't real
        NotificationModule.Message(PlayerWhoFired, 'That pet does not exist')
    end
end)

game.Players.PlayerAdded:Connect(function(Player)
    script.leaderstats:Clone().Parent = Player
    Player.CharacterAdded:Connect(function(character)
        -- give them their pet each time they respawn
        if PlayerPets[Player] then
            Assign(Player)
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
    -- clean up referance 
    PlayerPets[Player.Name] = nil
end)

But this part is the problem of the scrip is the problem:

local PetStorage = game:GetService('ServerStorage').Pets
local RemoteEvent = game:GetService("ReplicatedStorage").PetGiver
local NotificationModule = require ( script.Notification )
local Pets = {
    ['Cube'] = { Pet = PetStorage:WaitForChild("Cube"), Cost = 0 },
    ['Slime'] = { Pet = PetStorage:WaitForChild("Slime"), Cost = 1 }
}

If I keep it like this only the slime works.

local PetStorage = game:GetService('ServerStorage').Pets
local RemoteEvent = game:GetService("ReplicatedStorage").PetGiver
local NotificationModule = require ( script.Notification )
local Pets = {
    ['Cube'] = { Pet = PetStorage:WaitForChild("Cube"), Cost = 0 }
}

But if I change it to this only the Cube works. Copylocked version of my game

0
Sorry for the bad grammar. guesstroe 22 — 4y
0
Any error messages in the console? Those would be helpful if they are present. If not, what, exactly, is not working? IStarConquestI 414 — 4y
0
If I use the first script you can't buy the cube. guesstroe 22 — 4y

Answer this question