This script saves the diamonds value, but never the jumppower and rebirths values. what should i do to solve this problem?
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local DataStoreService = game:GetService("DataStoreService")
local CurrencyDataStore = DataStoreService:GetDataStore("Public")
local ConcurrentSaving = 0
local function SaveData(player)
ConcurrentSaving += 1
local leaderstats = player:FindFirstChild("leaderstats")
local JumpPower = leaderstats and leaderstats:FindFirstChild("JumpPower")
local Diamonds = leaderstats and leaderstats:FindFirstChild("Diamonds")
local Rebirths = leaderstats and leaderstats:FindFirstChild("Rebirths")
if JumpPower and Diamonds and Rebirths then
local data = {
JumpPower = JumpPower.Value,
Diamonds = Diamonds.Value,
Rebirths = Rebirths.Value
}
local success, error1 = pcall(function()
CurrencyDataStore:SetAsync(tostring(player.UserId), data)
end)
if not success then
warn("Fail:", error1)
end
end
ConcurrentSaving -= 1
end
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local JumpPower = Instance.new("IntValue")
JumpPower.Name = "JumpPower"
JumpPower.Parent = leaderstats
local Diamonds = Instance.new("IntValue")
Diamonds.Name = "Diamonds"
Diamonds.Parent = leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirths"
Rebirths.Parent = leaderstats
local success, result = pcall(function()
return CurrencyDataStore:GetAsync(tostring(player.UserId))
end)
if success and result then
JumpPower.Value = result.JumpPower or humanoid.JumpHeight
Diamonds.Value = result.Diamonds or 0
Rebirths.Value = result.Rebirths or 0
else
JumpPower.Value = humanoid.JumpHeight
Diamonds.Value = 0
Rebirths.Value = 0
end
local function onJumpPowerChanged(newValue)
JumpPower.Value = newValue
humanoid.JumpPower = newValue
end
JumpPower.Changed:Connect(onJumpPowerChanged)
player.CharacterRemoving:Connect(function()
SaveData(player)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
SaveData(player)
end)
local ConcurrentSaving = 0
game:BindToClose(function()
local MaxSaveAttempts = 3
local CurrentSaveAttempt = 0
while ConcurrentSaving > 0 and CurrentSaveAttempt < MaxSaveAttempts do
CurrentSaveAttempt += 1
task.wait(1)
end
end)
local player = game.Players.LocalPlayer
local JumpPower = player:WaitForChild("leaderstats"):WaitForChild("JumpPower")
local rebirths = player:WaitForChild("leaderstats"):WaitForChild("Rebirths")
local currentRebirth = 0
script.Parent.MouseButton1Click:Connect(function()
if JumpPower.Value >= 10 and currentRebirth == 0 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
currentRebirth = 1
elseif JumpPower.Value >= 100 and currentRebirth == 1 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
currentRebirth = 2
elseif JumpPower.Value >= 250 and currentRebirth == 2 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
currentRebirth = 3
elseif JumpPower.Value >= 400 and currentRebirth == 3 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
elseif JumpPower.Value >= 1200 and currentRebirth == 4 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
elseif JumpPower.Value >= 2500 and currentRebirth == 5 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
elseif JumpPower.Value >= 7500 and currentRebirth == 6 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
elseif JumpPower.Value >= 20000 and currentRebirth == 7 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
elseif JumpPower.Value >= 50000 and currentRebirth == 8 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
elseif JumpPower.Value >= 125000 and currentRebirth == 9 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
elseif JumpPower.Value >= 350000 and currentRebirth == 10 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
elseif JumpPower.Value >= 800000 and currentRebirth == 11 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
elseif JumpPower.Value >= 1000000 and currentRebirth == 12 then
rebirths.Value = rebirths.Value + 1
JumpPower.Value = 0
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpPower = 0.2
end
end
end)
Second script:
local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()
local debounce = false
-- JumpPower değerindeki değişiklikleri takip eden fonksiyon
local function onClick()
if debounce == false then
debounce = true
plr.leaderstats.JumpPower.Value += math.max(1, plr.leaderstats.Rebirths.Value * 10)
script.Parent.Parent.Sound:Stop()
script.Parent.Parent.Sound:Play()
debounce = false
end
end
local function onJumpPowerChanged(newValue)
char.Humanoid.JumpPower = newValue
end
-- JumpPower değeri değiştiğinde onJumpPowerChanged fonksiyonunu çalıştıran fonksiyon
plr.leaderstats.JumpPower.Changed:Connect(onJumpPowerChanged)
script.Parent.Activated:Connect(function()
onClick()
end)
mouse.Button1Down:Connect(function()
onClick()
end)
Datastores access the values on the server, in that script you will need to use a remote event to change the values on the server because on the server all the datastore is seeing is a value of 0 because it hasn’t been changed on the server. Here is an example of how to use it.
Server
remoteevent.OnServerEvent:Connect(function(player, valuename, value)
player.leaderstats[valuename].Value += value
end)
It’s not a good idea to code like you have in the “First script”, it makes the code bloated, difficult to read and debug, and impossible to update easily without editing intrinsic functions. Store this info in a table and use currentRebirth as the index into the table, i.e.:
local player = game.Players.LocalPlayer;
-- never use concatenated function calls, the return on the first call to `WaitForChild()` remains unchecked and you index it straight away without checking it!
local leaderstats = player:WaitForChild("leaderstats",3); -- make use of the second parameter to `WaitForChild()` since it is a timeout before continuing code execution (here I have given `WaitForChild()` 3 seconds to try to complete)
local JumpPower = leaderstats and leaderstats:WaitForChild("JumpPower",3);
local rebirths = leaderstats and leaderstats:WaitForChild("Rebirths",3);
-- now check rebirths because it was the last thing that indexed leaderstats, if any of the above allocations failed then it will propagate here and rebirths will be `nil`
if rebirths == nil then return end; -- not much we can do but return since anything below will break if anything above returned `nil`
local currentRebirth = 1; -- changed to 1 since Lua tables naturally begin at index 1
-- This is our table of values to check on. In memory it looks like this [1] = 10, [2] = 100, [3] = 400, etc.. So we can safely use currentRebirth to index it
local tableRebirths = {10,100,400,2500,20000,125000,350000,800000,1000000};
script.Parent.MouseButton1Click:Connect(function()
-- now all we have to do is check the given index (currentRebirth) against it's stored value (i.e. the 10, 100, 400, etc. values)
for index, value in pairs(tableRebirths) do
if JumpPower.Value >= value and currentRebirth == index then
rebirths.Value += 1;
JumpPower.Value = 0;
local humanoid = player.Character:FindFirstChildOfClass("Humanoid");
if humanoid then
humanoid.JumpPower = 0.2;
end
currentRebirth += 1;
break;
end
end
end)
Now to expand the rebirths up to 2 million or 50 billion all you have to do is add more numbers into `tableRebirths` and not having to cut and paste another elseif into that bloated function.