I was making a game, with a status system that includes some points to give away. I used roblox's DataStore service, but I'm having a problem with my game. After a player spends his points, distributing them in the stats he wants, after he leaves the game and re-enters those same points back to him with the same value, this can cause a point doubling glitch, which makes the unfair game, which I don't want. I think there is an error in my code, what can I do to fix it?
Code below:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlrData")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(Plr)
local data = Instance.new("Folder", Plr)
data.Name = "Data"
local exp = Instance.new("IntValue", data)
exp.Name = "Exp"
exp.Value = 1
local levels = Instance.new("IntValue", data)
levels.Name = "Level"
levels.Value = 1
local Points = Instance.new("IntValue", data)
Points.Name = "Points"
Points.Value = 3
local expneed = Instance.new("IntValue", data)
expneed.Name = "ExpNeeded"
expneed.Value = 100
local strenght = Instance.new("IntValue", data)
strenght.Value = 1
strenght.Name = "Strenght"
local Health = Instance.new("IntValue", data)
Health.Value = 100
Health.Name = "Health"
local char = Plr.Character or Plr.CharacterAdded:Wait()
local charhealth = char.Humanoid.Health
local DF = Instance.new("IntValue", data)
DF.Value = 1
DF.Name = "DF"
local PlayerId = Plr.UserId
local PlayerData = DataStore:GetAsync(PlayerId)
if PlayerData then
strenght.Value = PlayerData["Strenght"]
Health.Value = PlayerData["Health"]
DF.Value = PlayerData["DF"]
exp.Value = PlayerData["Exp"]
levels.Value = PlayerData["Level"]
expneed.Value = PlayerData["ExpNeeded"]
Points.Value = PlayerData["Points"]
charhealth = PlayerData["Health"]
end
end)
local function tab(Player)
local PlrStats = {}
for _, v in pairs(Player.Data:GetChildren()) do
PlrStats[v.Name] = v.Value
end
return PlrStats
end
game.Players.PlayerRemoving:Connect(function(Plr)
local PlrStats = tab(Plr)
local Sucess, Result = pcall(function()
local PlrId = Plr.UserId
DataStore:SetAsync(PlrId, PlrStats)
end)
if not Sucess then
print(Result)
warn("Error save")
end
end)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
task.wait()
local PlrStats = tab(Player)
local Sucess, Result = pcall(function()
local PlrId = Player.UserId
DataStore:SetAsync(PlrId, PlrStats)
end)
if not Sucess then
print(Result)
warn("Error")
end
end
end)
game.Players.PlayerAdded:Connect(function(Player)
task.wait(0.1)
local Level = Player.Data.Level
local Exp = Player.Data.Exp
local ExpNeed = Player.Data.ExpNeeded
local Points = Player.Data.Points
local Strenght = Player.Data.Strenght
local Health = Player.Data.Health
local DF = Player.Data:WaitForChild("DF")
RunService.Heartbeat:Connect(function()
if Exp.Value == 0 and ExpNeed.Value == 0 then
Exp.Value = 1
ExpNeed.Value = 100
end
if Points.Value > 300 then
Points.Value = 300
end
if Exp.Value >= ExpNeed.Value then
Exp.Value = Exp.Value - ExpNeed.Value
Level.Value += 1
ExpNeed.Value *= 1.13
Points.Value += 3
end
if Strenght.Value == 0 or DF.Value == 0 or Health.Value == 0 then
Strenght.Value = 1
DF.Value = 1
Health.Value = 1
end
if Strenght.Value > 100 or DF.Value > 100 then
Strenght.Value = 100
DF.Value = 100
end
if Health.Value > 1000 then
Health.Value = 1000
end
end)
RunService.Heartbeat:Connect(function()
if Level.Value >= 100 then
Level.Value = 100
end
end)
end)
Related
Sup guys, im working on level system in roblox studio. So i find a funny error when i spent all stats points and level up me added not 4 points but so many points like i didnt spend anything
Here the script:
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
local lvl = Instance.new("NumberValue", leaderstats)
lvl.Name = "Level"
lvl.Value = 1
local exp = Instance.new("NumberValue", leaderstats)
exp.Name = "Exp"
exp.Value = 0
local reqexp = Instance.new("NumberValue", Player)
reqexp.Name = "reqexp"
reqexp.Value = lvl.Value * 100
local hp = Instance.new("NumberValue", Player)
hp.Name = "Health"
hp.Value = 100
local points = Instance.new("NumberValue", Player)
points.Name = "Points"
points.Value = 0
exp.Changed:Connect(function(Changed)
if exp.Value >= reqexp.Value then
points.Value += 4
exp.Value = 0
lvl.Value += 1
reqexp.Value = lvl.Value * 100
end
end)
end)
I tried many ways but nothing help
Every value saves besides of StrMulti and probably EndMulti, ChakraMulti.
It just doesn't save at all. :c
(.............................................................................................)
(I need letters to post this. Ai doesn't want me to post this)
(Still can't post. Sorry for this extra text)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ServerStorage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local dataStore1 = DataStoreService:GetDataStore("Test")
local function waitForRequestBudget(requestType)
local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
while currentBudget < 1 do
currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
task.wait(5)
end
end
local function setupPlayerData(player: player)
local userID = player.UserId
local key = "Player_"..userID
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Multi = Instance.new("NumberValue", player)
Multi.Name = "Multi"
Multi.Value = 1
local rep = Instance.new("NumberValue", player)
rep.Name = "rep"
rep.Value = 0
local agZone = Instance.new("IntValue", player)
agZone.Name = "agZone"
agZone.Value = 1
local jfZone = Instance.new("IntValue", player)
jfZone.Name = "jfZone"
jfZone.Value = 1
local eMulti = Instance.new("NumberValue", player)
eMulti.Name = "eMulti"
eMulti.Value = 1
local Title = Instance.new("StringValue", leaderstats)
Title.Name = "Title"
Title.Value = "Innocent"
local eMultic = Instance.new("NumberValue", player)
eMultic.Name = "eMultic"
eMultic.Value = 1
local Agility = Instance.new("IntValue", player)
Agility.Name = "Agility"
Agility.Value = 90
local JumpForce = Instance.new("IntValue", player)
JumpForce.Name = "JumpForce"
JumpForce.Value = 0
local AgMulti = Instance.new("IntValue", player)
AgMulti.Name = "AgMulti"
AgMulti.Value = 1
local jfMulti = Instance.new("IntValue", player)
jfMulti.Name = "jfMulti"
jfMulti.Value = 1
local Chakra = Instance.new("IntValue", player)
Chakra.Name = "Chakra"
Chakra.Value = 0
local ZoneMulti = Instance.new("NumberValue", player)
ZoneMulti.Name = "ZoneMulti"
ZoneMulti.Value = 1
local eZoneMulti = Instance.new("NumberValue", player)
eZoneMulti.Name = "eZoneMulti"
eZoneMulti.Value = 1
local Strength = Instance.new("IntValue", player)
Strength.Name = "Strength"
Strength.Value = 100
local endZone = Instance.new("IntValue", player)
endZone.Name = "endZone"
endZone.Value = 1
local strZone = Instance.new("IntValue", player)
strZone.Name = "strZone"
strZone.Value = 1
local ChakraZone = Instance.new("IntValue", player)
ChakraZone.Name = "ChakraZone"
ChakraZone.Value = 1
local StrMulti = Instance.new("IntValue", player)
StrMulti.Name = "StrMulti"
StrMulti.Value = 1
local EndMulti = Instance.new("IntValue", player)
EndMulti.Name = "EndMulti"
EndMulti.Value = 1
local ChakraMulti = Instance.new("IntValue", player)
ChakraMulti.Name = "ChakraMulti"
ChakraMulti.Value = 1
local Endurance = Instance.new("IntValue", player)
Endurance.Name = "Endurance"
Endurance.Value = 0
local Yen = Instance.new("IntValue", player)
Yen.Name = "Yen"
Yen.Value = 0
local powers = Instance.new("Folder", player)
powers.Name = "Powers"
local hasAura = Instance.new("BoolValue", powers)
hasAura.Name = "hasAura"
hasAura.Value = true
local str4Q = Instance.new("NumberValue", player)
str4Q.Name = "Str4Q"
str4Q.Value = 0
local quest = Instance.new("IntValue", player)
quest.Name = "QuestNum"
quest.Value = 1
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
success, returnValue = pcall(dataStore1.GetAsync,dataStore1, key)
until success or not Players:FindFirstChild(player.Name)
success, returnValue = pcall(dataStore1.GetAsync,dataStore1, key)
if success then
if returnValue == nil then
returnValue = {
Endurance = 0,
Strength = 0,
Chakra = 0,
Yen = 0,
Agility = 0,
JumpForce = 0,
rep = 0,
ChakraMulti = 0,
StrMulti = 0,
EndMulti = 0
}
end
Strength.Value = if returnValue.Strength ~= nil then returnValue.Strength else 0
Yen.Value = if returnValue.Yen ~= nil then returnValue.Yen else 0
Endurance.Value = if returnValue.Endurance ~= nil then returnValue.Endurance else 0
Chakra.Value = if returnValue.Chakra ~= nil then returnValue.Chakra else 0
Agility.Value = if returnValue.Agility ~= nil then returnValue.Agility else 0
JumpForce.Value = if returnValue.JumpForce ~= nil then returnValue.JumpForce else 0
rep.Value = if returnValue.rep ~= nil then returnValue.rep else 0
ChakraMulti.Value = if returnValue.ChakraMulti ~= nil then returnValue.ChakraMulti else 0
EndMulti.Value = if returnValue.EndMulti ~= nil then returnValue.EndMulti else 0
StrMulti.Value = if returnValue.StrMulti ~= nil then returnValue.StrMulti else 0
else
player:Kick("There was an error loading your Data. Roblox's DataStore might be down, try again later, or contact us!")
print(player.Name.."Data loading Error!")
end
end
local function save(player)
local userID = player.UserId
local key = "Player_"..userID
local strength = player.Strength.Value
local yen = player.Yen.Value
local endurance = player.Endurance.Value
local chakra = player.Chakra.Value
local agility = player.Agility.Value
local jumpforce = player.JumpForce.Value
local Rep = player.rep.Value
local strmulti = player.StrMulti.Value
local endmulti = player.EndMulti.Value
local chakramulti = player.ChakraMulti.Value
local dataTable = {
Strength = strength,
Yen = yen,
Endurance = endurance,
Chakra = chakra,
Agility = agility,
JumpForce = jumpforce,
rep = Rep,
ChakraMulti = chakramulti,
EndMulti = endmulti,
StrMulti = strmulti
}
print(dataTable)
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
success, returnValue = pcall(dataStore1.UpdateAsync, dataStore1, key, function()
return dataTable
end)
until success
if success then
print("Data Saved!")
else
print("Data saving Error!")
end
end
local function onShutDown()
if RunService:IsStudio() then
task.wait(2)
else
local finished = Instance.new("BindableEvent")
local allPlayers = Players:GetPlayers()
local leftPlayers = #allPlayers
for _, player in ipairs(allPlayers) do
coroutine.wrap(function()
save(player)
leftPlayers -=1
if leftPlayers == 0 then
finished:Fire()
end
end)
end
finished.Event:Wait()
end
end
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(setupPlayerData)(player)
end
Players.PlayerAdded:Connect(setupPlayerData)
Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutDown)
while true do
task.wait(300)
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(save)(player)
end
end
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)
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()
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.