I’m new to Coding but I’m currently trying to see if i can save “Jump Power” in a game but I’m finding it quite difficult, I’ve made the script increment the amount of Jump power being added the player but i cant seem to wrap it all together to have it save. I’ve tried using SETASYNC Increment ASYNC to keep adding to the jump boost but i cant seem to save it.
CODE
local PlrInventory DataStoreService:GetDataStore("PlayerInventory")
local PlayerJump = DataStoreService:GetDataStore("PlayerInventory", "Jump")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.UseJumpPower = true
Character.Humanoid.JumpPower = 0
end)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Jumps = Instance.new("IntValue", leaderstats)
Jumps.Name = "Jumps"
local success, currentJump = pcall(function()
return PlayerJump:GetAsync(player.UserId)
end)
if success then
Jumps.Value = currentJump
else
Jumps.Value = 0
end
while true do
for _, player in pairs(game.Players:GetPlayers()) do
if player.Character then
player.Character.Humanoid.JumpPower += 3
end
end
local success, updatedJump = pcall(function()
return PlayerJump:SetAsync(player.UserId)
end)
if success then
Jumps.Value = currentJump
else
Jumps = 0
end
wait(0.3)
end
end)
any help on either optimizing my code or mistakes would be greatly appreciated
Thank you.
Looping while calling a datastore function like SetAsync is very wrong. You will quickly build up rate limits with this method, I suggest checking out and learning how to use ProfileService or ProfileStore which are great datastore management modules.
If you would like to do it yourself, set a key to every player (like "Player_" .. UserID) and manage all your data inside that key, you can save the key every minute or whenever the player is about to leave.
Okay so I read over it and had to almost redo everything. I set the increment to 3 every 1 second. It’s very basic data handling but it should work. If you got questions just ask
local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local JumpDS = DSS:GetDataStore("Jumps")
local Tracker = {}
local function OnJoin(Player:Player)
local Success, Data = pcall(JumpDS.GetAsync, JumpDS, Player.UserId)
if not Success then warn(Data) Player:Kick() return end
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Jumps = Instance.new("IntValue")
Jumps.Name = "Jumps"
Jumps.Value = Data or 0
Jumps.Parent = leaderstats
Player.CharacterAppearanceLoaded:Connect(function(character: Model)
local Humanoid = character:FindFirstChildOfClass("Humanoid")
Humanoid.UseJumpPower = true
Humanoid.JumpPower = Jumps.Value
end)
while task.wait(1) and Players:FindFirstChild(Player.Name) do
Jumps.Value += 3
Tracker[Player.UserId] = Jumps.Value
end
end
local function OnLeave(Player:Player)
local Data = Tracker[Player.UserId]
if not Data then return end
JumpDS:SetAsync(Player.UserId, Data)
Tracker[Player.UserId] = nil
end
Players.PlayerAdded:Connect(OnJoin)
Players.PlayerRemoving:Connect(OnLeave)