Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

DataStore help. It doesn't save my stats, how do I fix this?

Asked by 1 year ago

Hello so, I am trying to make a game on ROBLOX, pretty simple were each second you get +1 jump power, now I got the jump power thingy up and running, but I want to do so it saves when a player leaves and joins, so far I found this code on the internet, but nothing worked, here is the code.

local DataStores = game:GetService("DataStoreService") local FirstDataStore = DataStore:GetDataStore("JumpPowerStorage")

game.Players.PlayerAdded:connect(function(player) wait(0.1) player.Character.Humanoid.JumpPower = FirstDataStore:GetAsync(player.UserId) or 0 FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower) player.Character.Humanoid.Changed:connect(function() FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower) end) end) game.Players.PlayerRemoving:connect(function(player) FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower) end)

please help me fix it, thanks.

2 answers

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

Inside the variable FirstDataStore, you use "DataStore", and not the actual variable "DataStores". Replace DataStore with DataStores and you should be good to go.

local DataStores = game:GetService("DataStoreService")
local FirstDataStore = DataStores:GetDataStore("JumpPowerStorage")

game.Players.PlayerAdded:connect(function(player)
    wait(0.1)
    player.Character.Humanoid.JumpPower = FirstDataStore:GetAsync(player.UserId) or 0 
    FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower)
    player.Character.Humanoid.Changed:connect(function() 
        FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower) 
    end)
end)

game.Players.PlayerRemoving:connect(function(player) 
    FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower)
end)
0
it still doesn't save the jump power after rejoining BunnyRabitz 12 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

Try this

local DataStores = game:GetService("DataStoreService")
local FirstDataStore = DataStores:GetDataStore("JumpPowerStorage")

game.Players.PlayerAdded:Connect(function(player)
    wait(0.1)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.JumpPower = FirstDataStore:GetAsync(player.UserId) or 0
        humanoid.Changed:Connect(function()
            FirstDataStore:SetAsync(player.UserId, humanoid.JumpPower)
        end)
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    if player.Character then
        local humanoid = player.Character:WaitForChild("Humanoid")
        FirstDataStore:SetAsync(player.UserId, humanoid.JumpPower)
    end
end)

0
Hey, it works now, thank you very much. BunnyRabitz 12 — 1y

Answer this question