Argument 1 missing or nil error on roblox studio - lua

Hope you are having a good day. I am just here to say I have watched Alvinblox's egg hatching system tutorial and I am currently at part 2. There is a new error called "Argument 1 missing or nil" it is at line 60 in the petFollowPlayer script (local pet = game.ReplicatedStorage.HatchablePets:FindFirstChild(petName)) here is the code
petFollowPlayer
local function equipPet(player, pet, petName)
print(player)
local character = player.Character
if pet ~= nil and character ~= nil then
if character:FindFirstChild(player.Name.."s' pet") then
character(player.Name.."s' pet"):Destroy()
end
if character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then
character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy()
end
pet.Name = player.Name.."'s pet"
pet:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame)
local modelSize = pet.PrimaryPart.Size
local attachmentCharacter = Instance.new("Attachment")
attachmentCharacter.Visible = false
attachmentCharacter.Parent = character.HumanoidRootPart
attachmentCharacter.Position = Vector3.new(-1,0,1) + modelSize
attachmentCharacter.Name = "attachmentCharacter"
local attachmentPet = Instance.new("Attachment")
attachmentPet.Parent = pet.PrimaryPart
local alignPosition = Instance.new("AlignPosition")
alignPosition.MaxForce = 25000
alignPosition.Attachment0 = attachmentPet
alignPosition.Attachment1 = attachmentCharacter
alignPosition.Responsiveness = 25
alignPosition.Parent = pet
local alignOrientation = Instance.new("AlignOrientation")
alignOrientation.MaxTorque = 25000
alignOrientation.Attachment0= attachmentPet
alignOrientation.Attachment1= attachmentCharacter
alignOrientation.Responsiveness = 25
alignOrientation.Parent = pet
pet.Parent = character
end
end
game.Players.PlayerAdded:Connect(function(player)
local equipedPet = Instance.new("StringValue")
equipedPet.Name = "EquipedPet"
equipedPet.Parent = player
equipedPet.Value = ""
player.CharacterAdded:Connect(function(char)
if game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value) then
local clone =game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value):Clone()
equipPet(player,clone)
end
end)
equipedPet.Changed:Connect(function ()
if equipedPet.Value ~= nil then
if game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value) then
equipPet(player, game.ReplicatedStorage:WaitForChild("HatchablePets"):FindFirstChild(equipedPet.Value):Clone())
end
end
end)
end)
game.ReplicatedStorage.equipPet.OnServerEvent:Connect(function(player,petName)
local pet = game.ReplicatedStorage.HatchablePets:FindFirstChild(petName)
if pet and player.PetInventory:FindFirstChild(petName) then
player.EquipedPet.Value = petName
end
end)
game.ReplicatedStorage.unequipPet.OnServerEvent:Connect(function(player)
player.EquippedPet.Value = ""
if player.Character:FindFirstChild(player.Name.."'s name") then
player.Character[player.Name.."'s pet"]:Destroy()
end
if player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then
player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy()
end
end)
buyBasicEgg
local cost = 50
local petMoudle = require(game.ServerScriptService.BasicEggModule)
local prompt = workspace["Egg Holder"].Part.ProximityPrompt
prompt.Triggered:Connect(function (player)
if player.leaderstats.Cash.Value >= cost then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - cost
local pet = petMoudle.chooseRandomPet()
local petval = Instance.new("StringValue")
petval.Name = pet.Name
petval.Parent = player.PetInventory
print(pet.Name.." selected")
game.ReplicatedStorage.HatchEgg:FireClient(player, pet)
end
end)
basicEggAnimation
local camera = workspace.Camera
local studio = workspace.Studio
local template = script.Parent.Pets.ScrollingFrame.Template
local scrollingFrame = script.Parent.Pets.ScrollingFrame
local buttonConnection = {}
local function setTemplateEquipped()
for i,v in pairs(scrollingFrame:GetChildren()) do
if v:FindFirstChild("Equipped") then
v.Equipped.Text = "UNEQUIPPED"
v.Equipped.TextColor3 = Color3.fromRGB(255,0,0)
end
end
template.Equiped.Text = "Equipped"
template.Equiped.TextColor3 = Color3.fromRGB(0,255,0)
end
local function addToFrame(pet)
local newTemplate = template:Clone()
newTemplate.Name = pet.Name
newTemplate.petName.Text = pet.Name
newTemplate.Parent = scrollingFrame
newTemplate.Visible = true
local camera = Instance.new("Camera")
local newPet = pet:Clone()
newPet.Parent = newTemplate.ViewportFrame
camera.CFrame = CFrame.new(newPet.PrimaryPart.Position + (newPet.PrimaryPart.CFrame.LookVector * 3), newPet.PrimaryPart.Position)
camera.Parent = newTemplate.ViewportFrame
newTemplate.ViewportFrame.CurrentCamera = camera
buttonConnection[#buttonConnection+1] = newTemplate.MouseButton1Click:Connect(function()
if newTemplate.Equiped.Text == "EQUIPPED" then
game.ReplicatedStorage.unequipPet:FireServer(pet.Name)
newTemplate.Equiped.Text = "UNEQUIPPED"
newTemplate.Equiped.TextColor3 = Color3.fromRGB(255,0,0)
else
game.ReplicatedStorage.equipPet:FireServer()
setTemplateEquipped(newTemplate)
end
end)
end
game.ReplicatedStorage.HatchEgg.OnClientEvent:Connect(function (pet)
addToFrame(pet)
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = studio.CameraPart.CFrame
wait(1.5)
for i = 1,50,1 do
studio["Egg Mesh"].Size = studio["Egg Mesh"].Size + Vector3.new(0.1,0.1,0.1)
wait(0.01)
end
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 10
explosion.BlastPressure = 0
explosion.Position = studio["Egg Mesh"].Position
explosion.ExplosionType = Enum.ExplosionType.NoCraters
explosion.DestroyJointRadiusPercent = 0
explosion.Parent = studio["Egg Mesh"]
studio["Egg Mesh"].Transparency = 1
local petClone = pet:Clone()
for i,v in pairs(petClone:GetChildren()) do
if v:IsA("ParticleEmitter") then
v.Enabled = true
end
end
petClone:SetPrimaryPartCFrame(CFrame.new(studio["Egg Mesh"].Position, studio.CameraPart.Position))
petClone.Parent = studio
petClone.PrimaryPart.Position = Vector3.new(-14.382, -8.112, -605.565)
petClone.PrimaryPart.Anchored = true
local tweenInfo = TweenInfo.new(
2,
Enum.EasingStyle.Bounce,
Enum.EasingDirection.Out,
0,
false,
0
)
local TweenService = game:GetService("TweenService")
local targetVector = petClone.PrimaryPart.Position + (petClone.PrimaryPart.CFrame.lookVector * 5) + Vector3.new(0,0.75,0)
local lookVector = petClone.PrimaryPart.Position
local targetCframe = CFrame.new(targetVector, lookVector)
local propsToTween = { CFrame = targetCframe }
local tween = TweenService:Create(camera, tweenInfo, propsToTween)
tween:Play()
wait(5)
for i,v in pairs(petClone:GetChildren()) do
if v:IsA("ParticleEmitter") then
v.Enabled = false
end
end
camera.CameraType = Enum.CameraType.Custom
studio["Egg Mesh"].Transparency = 0
studio["Egg Mesh"].Size = Vector3.new(4.732, 6, 4.732)
petClone:Destroy()
end)
If you can help thank you so much it will be very appreciated :)

Looking at the line local pet = game.ReplicatedStorage.HatchablePets:FindFirstChild(petName)
The error is telling you that, for some reason, petName isn't defined. So looking at how the variable is defined, we need to check how game.ReplicatedStorage.equipPet is called. So looking in the addToFrame local function, you call it like this : game.ReplicatedStorage.equipPet:FireServer(). Your problem is that you haven't provided the pet name at all. So just do the same thing you did with the unequipPet event.
buttonConnection[#buttonConnection+1] = newTemplate.MouseButton1Click:Connect(function()
if newTemplate.Equiped.Text == "EQUIPPED" then
game.ReplicatedStorage.unequipPet:FireServer(pet.Name)
newTemplate.Equiped.Text = "UNEQUIPPED"
newTemplate.Equiped.TextColor3 = Color3.fromRGB(255,0,0)
else
game.ReplicatedStorage.equipPet:FireServer(pet.Name)
setTemplateEquipped(newTemplate)
end
end)

Related

Two Codes Confliting each other, how to fix it?

I made 2 codes of special abilitys and if i activate the SuperFireSphere code, then the Combat code damage stop working, the hitbox don't spawn in workspace and i don't know how to fix it.
SuperFireSphere Code:
local ts = game:GetService("TweenService")
local replicated = game.ReplicatedStorage
local onepiecepowers = replicated.OnePiecePowers
local Info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local ss = game:GetService("ServerStorage")
local FireWind = ss.FireWind:Clone()
local db = true
onepiecepowers.AceGiantFire.OnServerEvent:Connect(function(Player, Hit)
local char = Player.Character or Player.CharacterAdded:Wait()
local SuperFireSphere = Instance.new("Part")
local hrpp = char.PrimaryPart
local currentcam = workspace.Camera
currentcam.CameraSubject = char.Humanoid
currentcam.CameraType = Enum.CameraType.Scriptable
SuperFireSphere.Shape = Enum.PartType.Ball
SuperFireSphere.Material = Enum.Material.Neon
SuperFireSphere.BrickColor = BrickColor.new("New Yeller")
SuperFireSphere.Anchored = true
SuperFireSphere.CanCollide = false
task.wait(.2)
local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.Velocity = CFrame.lookAt(hrpp.CFrame.Position, Hit.Position).LookVector * 150
hrpp.Position += Vector3.new(0, 50 , 0)
hrpp.Anchored = true
SuperFireSphere.Parent = workspace
FireWind:SetPrimaryPartCFrame(hrpp.CFrame)
FireWind.Parent = workspace
SuperFireSphere.Position = char.PrimaryPart.Position + Vector3.new(0,15,0)
for i = 1, 15 do
task.wait(.1)
SuperFireSphere.Size += Vector3.new(2,2,2)
end
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
local tween = ts:Create(parts, Info, {Transparency = 1})
tween:Play()
end
end
local connect = SuperFireSphere.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= char.Humanoid and db == true then
db = false
hit.Parent.Humanoid.Health -= char.DevilFruitPoints.Value
task.wait(2)
db = true
end
end)
local FireParticle = ss.Fire:Clone()
FireParticle.Parent = SuperFireSphere
SuperFireSphere.Anchored = false
bv.Parent = SuperFireSphere
task.wait(2)
hrpp.Anchored = false
FireWind.Parent = ss
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
parts.Transparency = 0
end
end
task.wait(5)
SuperFireSphere:Destroy()
connect:Disconnect()
end)
Combat:
local replicated = game:GetService("ReplicatedStorage")
local combatevent = replicated.Combat
local servstore = game:GetService("ServerStorage")
local runservice = game:GetService("RunService")
local db = true
combatevent.OnServerEvent:Connect(function(Player)
local char = Player.Character or Player.CharacterAdded:Wait()
local hbox = Instance.new("Part")
hbox.Size = Vector3.new(1.5,1.5,1.5)
hbox.Anchored = true
hbox.CanCollide = false
local connection = runservice.Heartbeat:Connect(function(dt)
hbox.Position = char.RightHand.Position
end)
local tconnection = hbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= char.Humanoid and db then
db = false
hit.Parent.Humanoid.Health -= char.Strenght.Value
task.wait(.1)
db = true
end
end)
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://10497535355"
animation.Parent = char.Humanoid
local loadanim = char.Humanoid:LoadAnimation(animation)
loadanim:Play()
hbox.BrickColor = BrickColor.new("Really red")
hbox.Transparency = 0.5
hbox.Name = Player.Name
print(hbox.Name)
task.wait(.2)
hbox:Destroy()
tconnection:Disconnect()
loadanim:Stop()
connection:Disconnect()
end)
Does someone have any suggestion to help me to fix that??
.........................................................

How to make a code that shakes the camera of the player

I tried to make a code that's shake the camera player when a RemoteEvent is fired by a key on the keyboard. But it didn't worked. I tried to use the CameraOffset but didn't worked too. How can i make this code without using the CameraShakeModule??? Any sugestion??? Code below:
local ts = game:GetService("TweenService")
local replicated = game.ReplicatedStorage
local powers = replicated.Powers
local Info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local ss = game:GetService("ServerStorage")
local FireWind = ss.FireWind:Clone()
powers.GiantFire.OnServerEvent:Connect(function(Player, Hit)
local char = Player.Character or Player.CharacterAdded:Wait()
local SuperFireSphere = Instance.new("Part")
local hrpp = char.PrimaryPart
local currentcam = workspace.Camera
currentcam.CameraSubject = char.Humanoid
currentcam.CameraType = Enum.CameraType.Scriptable
SuperFireSphere.Shape = Enum.PartType.Ball
SuperFireSphere.Material = Enum.Material.Neon
SuperFireSphere.BrickColor = BrickColor.new("New Yeller")
SuperFireSphere.Anchored = true
SuperFireSphere.CanCollide = false
task.wait(.2)
local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.Velocity = CFrame.lookAt(hrpp.CFrame.Position, Hit.Position).LookVector * 150
hrpp.Position += Vector3.new(0, 50 , 0)
hrpp.Anchored = true
SuperFireSphere.Parent = workspace
FireWind:SetPrimaryPartCFrame(hrpp.CFrame)
FireWind.Parent = workspace
SuperFireSphere.Position = char.PrimaryPart.Position + Vector3.new(0,15,0)
for i = 1, 15 do
task.wait(.1)
SuperFireSphere.Size += Vector3.new(2,2,2)
end
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
local tween = ts:Create(parts, Info, {Transparency = 1})
tween:Play()
end
end
for i = 1,20 do
local x = math.random(-100, 100)/100
local y = math.random(-100, 100)/100
local z = math.random(-100, 100)/100
char.Humanoid.CameraOffset = Vector3.new(x,y,z)
print(i)
task.wait(.5)
end
local FireParticle = ss.Fire:Clone()
FireParticle.Parent = SuperFireSphere
SuperFireSphere.Anchored = false
bv.Parent = SuperFireSphere
task.wait(2)
hrpp.Anchored = false
FireWind.Parent = ss
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
parts.Transparency = 0
end
end
task.wait(5)
SuperFireSphere:Destroy()
end)
Your problem is that you made the CameraType scriptable, with it scriptable, the character's Humanoid no longer has control of it. Remove the lines that make the CameraType scriptable and it will solve your issue.

There is an error "Positon is not a valid member of Part "Workspace.Studio.Mouse.hitbox" which is strange

I have watched Alvinblox's egg hatching tutorial and once again there is an error :/
the error is "Positon is not a valid member of Part "Workspace.Studio.Mouse.hitbox""
Here is the code below if you can help me thank you so much!
local camera = workspace.Camera
local studio = workspace.Studio
local template = script.Parent.Pets.ScrollingFrame.Template
local scrollingFrame = script.Parent.Pets.ScrollingFrame
local function addToFrame(pet)
local newTemplate = template:Clone()
newTemplate.Name = pet.Name
newTemplate.petName.Text = pet.Name
newTemplate.Parent = scrollingFrame
newTemplate.Visible = true
local camera = Instance.new("Camera")
local newPet = pet:Clone()
newPet.Parent = newTemplate.ViewportFrame
camera.CFrame = CFrame.new(newPet.PrimaryPart.Position + (newPet.PrimaryPart.CFrame.LookVector * 3), newPet.PrimaryPart.Position)
camera.Parent = newTemplate.ViewportFrame
newTemplate.ViewportFrame.CurrentCamera = camera
end
game.ReplicatedStorage.HatchEgg.OnClientEvent:Connect(function (pet)
addToFrame(pet)
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = studio.CameraPart.CFrame
wait(1.5)
for i = 1,50,1 do
studio["Egg Mesh"].Size = studio["Egg Mesh"].Size + Vector3.new(0.1,0.1,0.1)
wait(0.01)
end
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 10
explosion.BlastPressure = 0
explosion.Position = studio["Egg Mesh"].Position
explosion.ExplosionType = Enum.ExplosionType.NoCraters
explosion.DestroyJointRadiusPercent = 0
explosion.Parent = studio["Egg Mesh"]
studio["Egg Mesh"].Transparency = 1
local petClone = pet:Clone()
for i,v in pairs(petClone:GetChildren()) do
if v:IsA("ParticleEmitter") then
v.Enabled = true
end
end
petClone:SetPrimaryPartCFrame(CFrame.new(studio["Egg Mesh"].Position, studio.CameraPart.Position))
petClone.Parent = studio
local tweenInfo = TweenInfo.new(
2,
Enum.EasingStyle.Bounce,
Enum.EasingDirection.Out,
0,
false,
0
)
local tween = game:GetService("TweenService"):Create(camera, TweenInfo, {CFrame = CFrame.new(petClone.PrimaryPart.Positon + (petClone.PrimaryPart.CFrame.lookVector * 5)+Vector3.new(0,0.75,0)),petClone.PrimaryPart.Position})
tween:Play()
wait(5)
for i,v in pairs(petClone:GetChildren()) do
if v:IsA("ParticleEmitter") then
v.Enabled = false
end
end
camera.CameraType = Enum.CameraType.Custom
studio["Egg Mesh"].Transparency = 0
studio["Egg Mesh"].Size = Vector3.new(4.732, 6, 4.732)
end)
Here is the fireserver code
local cost = 50
local petMoudle = require(game.ServerScriptService.BasicEggModule)
local prompt = workspace["Egg Holder"].Part.ProximityPrompt
prompt.Triggered:Connect(function (player)
if player.leaderstats.Cash.Value >= cost then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - cost
local pet = petMoudle.chooseRandomPet()
print(pet.Name.." selected")
game.ReplicatedStorage.HatchEgg:FireClient(player, pet)
end
end)
Here is the module
local petMoudle = {}
petMoudle.Pets = {
["Legendary"] = {
game.ReplicatedStorage.HatchablePets["lil' demon"];
};
["Rare"] = {
game.ReplicatedStorage.HatchablePets["Ice golem"];
game.ReplicatedStorage.HatchablePets["Light Bat"];
};
["Uncommon"] = {
game.ReplicatedStorage.HatchablePets.Fox
};
["Common"] = {
game.ReplicatedStorage.HatchablePets.Bunny;
game.ReplicatedStorage.HatchablePets.Mouse
}
}
petMoudle.rarities = {
["Legendary"] = 5;
["Rare"] = 15;
["Uncommon"] = 30;
["Common"] = 50
}
petMoudle.chooseRandomPet = function()
local randomNumber = math.random(1, 100)
local counter = 0
for rarity, weight in pairs(petMoudle.rarities) do
counter = counter + weight
if randomNumber <= counter then
local rarityTable = petMoudle.Pets[rarity]
local chosenPet = rarityTable[math.random(1, #rarityTable)]
return chosenPet
end
end
end
return petMoudle
Once again thank you if you can help me

Unable to cast to Dictionary error on lua

I have watched Alvinblox's egg hatching tutorial and FOR THE 3RD TIME there is an error :/
the error is "Unable to cast to Dictionary" It is at line 53 (the longest line)
Here is the code below if you can help me thank you so much!
local camera = workspace.Camera
local studio = workspace.Studio
local template = script.Parent.Pets.ScrollingFrame.Template
local scrollingFrame = script.Parent.Pets.ScrollingFrame
local function addToFrame(pet)
local newTemplate = template:Clone()
newTemplate.Name = pet.Name
newTemplate.petName.Text = pet.Name
newTemplate.Parent = scrollingFrame
newTemplate.Visible = true
local camera = Instance.new("Camera")
local newPet = pet:Clone()
newPet.Parent = newTemplate.ViewportFrame
camera.CFrame = CFrame.new(newPet.PrimaryPart.Position + (newPet.PrimaryPart.CFrame.LookVector * 3), newPet.PrimaryPart.Position)
camera.Parent = newTemplate.ViewportFrame
newTemplate.ViewportFrame.CurrentCamera = camera
end
game.ReplicatedStorage.HatchEgg.OnClientEvent:Connect(function (pet)
addToFrame(pet)
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = studio.CameraPart.CFrame
wait(1.5)
for i = 1,50,1 do
studio["Egg Mesh"].Size = studio["Egg Mesh"].Size + Vector3.new(0.1,0.1,0.1)
wait(0.01)
end
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 10
explosion.BlastPressure = 0
explosion.Position = studio["Egg Mesh"].Position
explosion.ExplosionType = Enum.ExplosionType.NoCraters
explosion.DestroyJointRadiusPercent = 0
explosion.Parent = studio["Egg Mesh"]
studio["Egg Mesh"].Transparency = 1
local petClone = pet:Clone()
for i,v in pairs(petClone:GetChildren()) do
if v:IsA("ParticleEmitter") then
v.Enabled = true
end
end
petClone:SetPrimaryPartCFrame(CFrame.new(studio["Egg Mesh"].Position, studio.CameraPart.Position))
petClone.Parent = studio
local tweenInfo = TweenInfo.new(
2,
Enum.EasingStyle.Bounce,
Enum.EasingDirection.Out,
0,
false,
0
)
local tween = game:GetService("TweenService"):Create(camera, TweenInfo, {CFrame = CFrame.new(petClone.PrimaryPart.Positon + (petClone.PrimaryPart.CFrame.lookVector * 5)+Vector3.new(0,0.75,0)),petClone.PrimaryPart.Position})
tween:Play()
wait(5)
for i,v in pairs(petClone:GetChildren()) do
if v:IsA("ParticleEmitter") then
v.Enabled = false
end
end
camera.CameraType = Enum.CameraType.Custom
studio["Egg Mesh"].Transparency = 0
studio["Egg Mesh"].Size = Vector3.new(4.732, 6, 4.732)
end)
Here is the fireserver code
local cost = 50
local petMoudle = require(game.ServerScriptService.BasicEggModule)
local prompt = workspace["Egg Holder"].Part.ProximityPrompt
prompt.Triggered:Connect(function (player)
if player.leaderstats.Cash.Value >= cost then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - cost
local pet = petMoudle.chooseRandomPet()
print(pet.Name.." selected")
game.ReplicatedStorage.HatchEgg:FireClient(player, pet)
end
end)
Here is the module
local petMoudle = {}
petMoudle.Pets = {
["Legendary"] = {
game.ReplicatedStorage.HatchablePets["lil' demon"];
};
["Rare"] = {
game.ReplicatedStorage.HatchablePets["Ice golem"];
game.ReplicatedStorage.HatchablePets["Light Bat"];
};
["Uncommon"] = {
game.ReplicatedStorage.HatchablePets.Fox
};
["Common"] = {
game.ReplicatedStorage.HatchablePets.Bunny;
game.ReplicatedStorage.HatchablePets.Mouse
}
}
petMoudle.rarities = {
["Legendary"] = 5;
["Rare"] = 15;
["Uncommon"] = 30;
["Common"] = 50
}
petMoudle.chooseRandomPet = function()
local randomNumber = math.random(1, 100)
local counter = 0
for rarity, weight in pairs(petMoudle.rarities) do
counter = counter + weight
if randomNumber <= counter then
local rarityTable = petMoudle.Pets[rarity]
local chosenPet = rarityTable[math.random(1, #rarityTable)]
return chosenPet
end
end
end
return petMoudle
Once again thank you if you can help me
The TweenService:Create function expects a dictionary for the last argument, and you have provided this (simplified):
{ CFrame = CFrame.new(...), petClone.PrimaryPart.Position }
The error you are getting is telling you that the table you have created has mixed keys, and that cannot be cast to just a dictionary. But all of this is because your actual issue was a misplaced close parenthesis. It's supposed to be...
CFrame.new(..., petClone.PrimaryPart.Position)
Now, let's fix it and break up this line into more readable parts...
local TweenService = game:GetService("TweenService")
local targetVector = petClone.PrimaryPart.Positon + (petClone.PrimaryPart.CFrame.lookVector * 5) + Vector3.new(0,0.75,0)
local lookVector = petClone.PrimaryPart.Position
local targetCframe = CFrame.new(targetVector, lookVector)
local propsToTween = { CFrame = targetCframe }
local tween = TweenService:Create(camera, tweenInfo, propsToTween)
tween:Play()

Not doing the random pet, just giving the rarest every time. How do I fix it?

I am making a simulator game on roblox and it has a pet system. I revamped how the hatch works, and for some reason it started to glitch out, and always give the same pet, and the pet was always the rarest one. I don't know what the problem is, here is the script. The Weight system worked perfectly before, but now it isnt working. I compared it to the old version of the game and the only difference is what the variables are called.
game.ReplicatedStorage.OpenEgg.OnServerInvoke = (function(player, amount, egg)
print("Fired")
if not player:FindFirstChild("Debounce") then
if amount == "One" then
local CC = player.leaderstats.CloudCoins
local EO = player.leaderstats.EggsOpened
local EGGS = require(game.ReplicatedStorage.EGGS)
local Data = EGGS[egg]
local Price = Data["Cost"]
local Pets = Data["Pets"]
local TotalWeight = 0
for i,v in pairs(Pets) do
TotalWeight = TotalWeight + v[1]
end
local function ChoosePet(player)
local Chance = math.random(1,TotalWeight)
local Counter = 0
for d,c in pairs(Pets)do
Counter = Counter + c[1]
if Chance >= Counter then
return d
end
end
end
local function GetChance(ChosenPet)
for i, v in pairs(Pets)do
for o,b in pairs(Pets) do
if o == ChosenPet then
return b[1]
end
end
end
end
if CC.Value >= Price then
local ChosenPet = ChoosePet(player)
local Chance = GetChance(ChosenPet)
CC.Value = CC.Value - Price
EO.Value = EO.Value + 1
local Pet = Instance.new("BoolValue")
Pet.Name = ChosenPet
Pet.Parent = player.Pets
if not player.PetsA:FindFirstChild(Pet.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
local Level = Instance.new("IntValue")
local Xp = Instance.new("IntValue")
local Number = Instance.new("IntValue")
Number.Name = "Number"
Number.Value = player.PetsA[Pet.Name.." Amount"].Value
Number.Parent = Pet
Level.Name = "Level"
Xp.Name = "XP"
Level.Value = 1
Xp.Value = 0
Level.Parent = Pet
Xp.Parent = Pet
return("Hatched")
else
return("NotEnoughCoins")
end
end
if amount == "Triple" then
local CC = player.leaderstats.CloudCoins
local EO = player.leaderstats.EggsOpened
local EGGS = require(game.ReplicatedStorage.EGGS)
local Data = EGGS[egg]
local Price = Data["Cost"]
local Pets = Data["Pets"]
local TotalWeight = 0
for i,v in pairs(Pets) do
TotalWeight = TotalWeight + v[1]
end
local function ChoosePet(player)
local Chance = math.random(1,TotalWeight)
local Counter = 0
for d,c in pairs(Pets)do
Counter = Counter + c[1]
if Chance >= Counter then
return d
end
end
end
local function GetChance(ChosenPet)
for i, v in pairs(Pets)do
for o,b in pairs(Pets) do
if o == ChosenPet then
return b[1]
end
end
end
end
if CC.Value >= Price*3 then
local ChosenPet1,ChosenPet2,ChosenPet3 = ChoosePet(player),ChoosePet(player),ChoosePet(player)
local Chance1,Chance2,Chance3 = GetChance(ChosenPet1),GetChance(ChosenPet2),GetChance(ChosenPet3)
CC.Value = CC.Value - Price*3
EO.Value = EO.Value + 3
local Pet1 = Instance.new("BoolValue")
Pet1.Name = ChosenPet1
Pet1.Parent = player.Pets
local Level1 = Instance.new("IntValue")
local Xp1 = Instance.new("IntValue")
local Number1 = Instance.new("IntValue")
Number1.Name = "Number"
if not player.PetsA:FindFirstChild(Pet1.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet1.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet1.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
Number1.Value = player.PetsA[Pet1.Name.." Amount"].Value
Number1.Parent = Pet1
Level1.Name = "Level"
Xp1.Name = "XP"
Level1.Value = 1
Xp1.Value = 0
Level1.Parent = Pet1
Xp1.Parent = Pet1
local Pet2 = Instance.new("BoolValue")
Pet2.Name = ChosenPet2
Pet2.Parent = player.Pets
local Level2 = Instance.new("IntValue")
local Xp2 = Instance.new("IntValue")
local Number2 = Instance.new("IntValue")
Number2.Name = "Number"
if not player.PetsA:FindFirstChild(Pet2.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet2.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet2.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
Number2.Value = player.PetsA[Pet2.Name.." Amount"].Value
Number2.Parent = Pet2
Level2.Name = "Level"
Xp2.Name = "XP"
Level2.Value = 1
Xp2.Value = 0
Level2.Parent = Pet2
Xp2.Parent = Pet2
local Pet3 = Instance.new("BoolValue")
Pet3.Name = ChosenPet3
Pet3.Parent = player.Pets
local Level3 = Instance.new("IntValue")
local Xp3 = Instance.new("IntValue")
local Number3 = Instance.new("IntValue")
Number3.Name = "Number"
if not player.PetsA:FindFirstChild(Pet3.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet3.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet3.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
Number3.Value = player.PetsA[Pet3.Name.." Amount"].Value
Number3.Parent = Pet3
Level3.Name = "Level"
Xp3.Name = "XP"
Level3.Value = 1
Xp3.Value = 0
Level3.Parent = Pet3
Xp3.Parent = Pet3
return("Hatched")
else
return("NotEnoughCoins")
end
end
end
end)
take a closer look at these lines of code.
local TotalWeight = 0
for i,v in pairs(Pets) do
TotalWeight = TotalWeight + v[1]
end
local function ChoosePet(player)
local Chance = math.random(1,TotalWeight)
local Counter = 0
for d,c in pairs(Pets)do
Counter = Counter + c[1]
if Chance >= Counter then
return d
end
end
end
the next time we see this function it is to assign the chosen pet to the player. I would suggest playing around with the Chance value. Set it to something crazy and see if you get another pet instead.

Resources