choosePet()
returns nothing.
Same errorâŚ
I suggest you using math.random(0, totalWeight)
,
Because some of the pet rarities are below 1 (math.random(1, 0.01) has no interval), so the interval error occurs.
Try doing that to avoid it.
That could be the reason function choosePet()
returns nil
Same error D :
Is Handler a LocalScript that has PetHatched.OnClientEvent?
Pardon me, I didnât see that
I ran your edited code and everything worked fine!
Can you show me the scripts that are edited by you again, so I can find any mistakes?
Yes sure!
local script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketPlaceService = game:GetService("MarketplaceService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local EggsFolder = workspace.Eggs
local PetModels = ReplicatedStorage:WaitForChild("Pets")
local EggConfig = require(ReplicatedStorage:WaitForChild("Config"):WaitForChild("EggsConfig"))
local BuyEgg = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("BuyEgg")
local PetHatched = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("PetHatched")
local template = script.Parent.Template
local function GetRarityColor(rarity: string)
local color
if rarity == "Common" then
color = Color3.fromRGB(173, 173, 173)
elseif rarity == "Uncommon" then
color = Color3.fromRGB(9, 176, 0)
elseif rarity == "Rare" then
color = Color3.fromRGB(19, 193, 164)
elseif rarity == "Secret" then
color = Color3.fromRGB(0, 0, 0)
end
return color
end
local function GetRarity(rarity: string)
local rarrity
if rarity == "Common" then
rarrity = "Common"
elseif rarity == "Uncommon" then
rarrity = "Uncommon"
elseif rarity == "Rare" then
rarrity = "Rare"
elseif rarity == "Secret" then
rarrity = "??"
end
return rarrity
end
local function generateViewportFrame(viewportFrame: ViewportFrame, petModel: Model)
petModel:PivotTo(CFrame.new() * CFrame.Angles(0, math.rad(180), 0))
petModel.Parent = viewportFrame
local camera = Instance.new("Camera", viewportFrame)
viewportFrame.CurrentCamera = camera
camera.CFrame = CFrame.new(Vector3.new(-1,1,4), petModel.PrimaryPart.Position)
if petModel.Name == "GiantDoge" or "GiantChicken" then
camera.FieldOfView = 50
else
camera.FieldOfView = 30
end
end
local function generatePet(container: Frame, petConfig: table)
local clone = container.Parent.Parent.Parent.Templatee:Clone()
clone.Parent = container
clone.Name = petConfig.ID
clone.Visible = true
clone.PetName.Text = petConfig.ID:gsub("_", " ")
clone.Rarity.Text = GetRarity(petConfig.Rarity)
clone.Rarity.TextColor3 = GetRarityColor(petConfig.Rarity)
local petClone = PetModels[petConfig.ID]:Clone()
generateViewportFrame(clone.ViewportFrame, petClone)
clone.MouseEnter:Connect(function()
if petConfig.Chance > 1 then
clone.Rarity.Text = petConfig.Chance.."%"
else
clone.Rarity.Text = "??"
end
end)
clone.MouseLeave:Connect(function()
if petConfig.Rarity == "Secret" then
clone.Rarity.Text = "??"
elseif petConfig.Rarity ~= "Secret" then
clone.Rarity.Text = petConfig.Rarity
end
end)
end
local function requestHatch(eggId: string, action: string)
if action == "Auto" then
BuyEgg:fireServer(eggId, "Auto")
elseif action == "x1" then
BuyEgg:FireServer(eggId, "x1")
elseif action == "x3" then
BuyEgg:FireServer(eggId, "x3")
end
end
local function generateBillGui(eggModel: Instance, eggConfig: table)
local attachment = eggModel.Attachment
local clone = template:Clone()
clone.Parent = script.Parent
clone.Adornee = attachment
clone.Name = eggModel.Name
clone.Frame.Title.TextLabel.Text = eggModel.Name:gsub("_", " ")
clone.Frame.Display.TextLabel.Text = eggConfig.Price
for _, pet in ipairs(eggConfig.Pets) do
generatePet(clone.Frame.Container, pet)
end
clone.Frame.BtnsHolder.Buy1.MouseButton1Click:Connect(function()
requestHatch(eggModel.Name, "x1")
end)
clone.Frame.BtnsHolder.Buy3.MouseButton1Click:Connect(function()
requestHatch(eggModel.Name, "x3")
end)
clone.Frame.BtnsHolder.AutoBuy.MouseButton1Click:Connect(function()
requestHatch(eggModel.Name, "Auto")
end)
end
for eggName, eggConfig in pairs(EggConfig) do
generateBillGui(EggsFolder[eggName], eggConfig )
end
local function getClosestEgg()
local closest = {Egg = nil, Distance = 9_999}
for _, egg in ipairs(EggsFolder:GetChildren()) do
local distanceBetween = (Player.Character.PrimaryPart.Position - egg.Position).Magnitude
if distanceBetween > 17 then continue end
if closest.Distance > distanceBetween then
closest = {Egg = egg.Name, Distance = distanceBetween}
end
end
return closest.Egg
end
UserInputService.InputEnded:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then return end
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
local eggId = getClosestEgg()
if not eggId then return end
if input.KeyCode == Enum.KeyCode.T then
requestHatch(eggId, "Auto")
elseif input.KeyCode == Enum.KeyCode.E then
requestHatch(eggId, "x1")
elseif input.KeyCode == Enum.KeyCode.R then
requestHatch(eggId, "x3")
end
end)
PetHatched.OnClientEvent:Connect(function(player, pet: table)
print("You got a "..pet.Rarity.." "..pet.name)
end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EggsFolder = workspace.Eggs
local EggsConfig = require(ReplicatedStorage:FindFirstChild("Config"):FindFirstChild("EggsConfig"))
local BuyEgg = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("BuyEgg")
local PetHatched = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("PetHatched")
local HATCH_COOLDOWN = 3
local HatchCooldown = {}
local function choosePet(pets: table)
local totalWeight = 0
for _, pet in ipairs(pets) do
totalWeight += pet.Chance
end
local chance = math.random(0, totalWeight)
local counter = 0
for _, pet in ipairs(pets) do
counter += pet.Chance
if chance <= counter then
return pet
end
end
end
local function hatch(player: Player, eggId: string)
if HatchCooldown[player.UserId] then return end
local eggConfig = EggsConfig[eggId]
local playerBalance = player.leaderstats.Coins.Value
if playerBalance < eggConfig.Price then return end
HatchCooldown[player.UserId] = true
player.leaderstats.Coins.Value -= eggConfig.Price
local pet = choosePet(eggConfig.Pets)
PetHatched:FireClient(player, pet)
task.delay(HATCH_COOLDOWN, function()
HatchCooldown[player.UserId] = nil
end)
end
local function canHatch(player: Player, eggId: string)
local eggConfig = EggsConfig[eggId]
if not eggConfig then return false end
local playerBalance = player.leaderstats.Coins.Value
return playerBalance >= eggConfig.Price
end
BuyEgg.OnServerEvent:Connect(function(player: Player, eggId: string, action: string)
if not canHatch(player, eggId) then return end
if action == "Auto" then
--
elseif action == "x3" then
--
else
hatch(player, eggId)
end
end)
Put print(pet)
for debugging.
remove the player argument from this onclientevent, it will make the pet table nil otherwise and cause an error
Thank you.
I forgot that I removed that âplayerâ argument when I edited it.
I still have this error, REMEMBER that this is the 2nd egg that gives the error! The 1st one works fine!!!
The 2nd egg has a problem, the template dowsnât clone on the container!
Thanks!!
Also thanks but how i just said to the other person⌠i still have the problem!
Thanks!
Can you send a screenshot of the line causing the error?
Yes, sure!
Btw, i need to eat dinner, so i canât reply now, maybe in the nexty 10 minutes
Thanks!
Alright, I see the problem.
Line 164 is missing a player variable when you get the client event. Your code is thinking that pet: table is the player. You should have something like this on line 164.
PetHatched.OnClientEvent:Connect(function(player, pet: table)
There could be a chance that the pet could wind up being nil, add a debug print to make sure the choosePet function is actually returning a pet rather than nil
In the second egg, Pets isnât a table that contains other pets,
Instead it contains pet stats.
Pets = {
ID = "Pig", name = "Pig", Rarity = "Uncommon", Chance = 50
},
{
ID = "Dog", name = "Dog", Rarity = "Uncommon", Chance = 50
}
Try this:
Pets = {
{
ID = "Pig", name = "Pig", Rarity = "Uncommon", Chance = 50
},
{
ID = "Dog", name = "Dog", Rarity = "Uncommon", Chance = 50
}
}
Thank you so much! I didnât saw that small mistakeâŚ
I wish i could give to the other person the âSolutionâ but i canâtâŚ
Thanks to all!!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.