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

How can I save and reload JumpPower and walkspeed in games?

Asked by 6 years ago

I dont understand how to use datastores whatsoever and was wondering if there was another way to save humanoid content in a game as well as reloading it on join.

0
Use two NumberValues, set the two NumberValues's Values to the WalkSpeed and JumpPower values and save the values of the two NumberValues(Sorry if that was a bit confusing lol) MusicalDisplay 173 — 6y
0
Thanks but I have just started scripting and have no idea what I am doing. Thanks though. :) itsdiplexx 9 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Basically, you're going to have to learn data store. if the roblox wiki doesn't explain it well enough i can explain it more specifically (if you cant be bothered reading, skip to the bottom for full code)

So** first **you need to import the **Datastore **service. you dont need to know what it does. you just need to have this line of code in every script you require data storage:

local DataStores = game:GetService("DataStoreService")

So First thing you need to know is the data store. So DataStores is like a Main warehouse that holds warehouses because it holds all the different datastores

First you need to make a DataStore. so what you use is the :GetDataStore() which gets the Datastore if its already been created or if it hasn't will create one for you.

So you use this line of code

local FirstDataStore = DataStore:GetDataStore("WalkSpeedStorage")

FirstDataStore is a normal variable, it can be called what ever you want WalkSpeedStorage is what the FirstDataStore is named. you can name it whatever you want.

If this is confusing then think back to the warehouse example. the WalkSpeedStorage is the name of the warehouse inside of the DataStores

this is the code so far:

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

So DataStores now contains a Datastore called "WalkSpeedStorage"

WalkSpeedStorage is where we are going to store everyones walkspeed

So Each and every player has a unique UserID this is going to be our Password to access our own private walkspeed values from the walkspeed storage. this key can be used to access their own personal data from any datastore.

So When a player joins for the first time. WalkSpeedStorage doesnt have the players UserID as a Key yet because they have never had a saved walkspeed. so we use this code:

game.Players.PlayerAdded:connect(function(player)
wait(0.1)
player.Character.Humanoid.WalkSpeed = FirstDataStore:GetAsync(player.UserId) or 16 
FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.WalkSpeed)

So this code means that when a player joins Their characters walkspeed is changed to their saved walkspeed.

Where it says "FirstDataStore:GetAsync(player.UserId) or 16 ", "player.UserId", is where the walkspeed was stored last time the player left the game. if the player has never played before it wont be able to find a value. thats why we have the "or" to set it to 16.

Then we add:

player.Character.Humanoid.Changed:connect(function()
    FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.WalkSpeed)
end)

This code makes it so that everytime walkspeed is changed. the Data will be stored to the Players UserId for the WalkSpeedStorage.

Full code so far:

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

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

So far we have made it so:

1. we find the WalkSpeedStorage in the DataStore

2. we wait for player to join

3. then we set walkspeed of the player to its previous walkspeed found in the WalkSpeedStorage. (or set the walkspeed to 16 then save it if the player is playing for the first time)

4. We save the walkspeed to the WalkSpeedStorage every-time it changes

Now For safety measures we want to store the walkspeed a second time if the player decides to leave so outside the player joins function we are going to have a player leaves function that sets the walkspeed to the players UserId

game.Players.PlayerRemoving:connect(function(player) FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.WalkSpeed) end)

so the whole script should look like this

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

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

Btw this is for the WalkSpeed Storage alone. you will have to make one for jump storage because idk where you save the jump storage to. i hope i have boosted your understand of datastorage so you can write the next one yourself ~ AnAnonymousDeveloper

0
Absolute LEGEND TYSM itsdiplexx 9 — 6y
0
nw PM me if you need anything else AnAnonymousDeveloper 77 — 6y
0
wait, character is a nil value though. itsdiplexx 9 — 6y
0
oops AnAnonymousDeveloper 77 — 6y
View all comments (4 more)
0
put "player.CharacterAdded:connect(function(character)" between AnAnonymousDeveloper 77 — 6y
0
put "player.CharacterAdded:connect(function(character)" between AnAnonymousDeveloper 77 — 6y
0
put "player.CharacterAdded:connect(function(character)" between "game.Players.PlayerAdded:connect(function(player)" and "wait(0.1)" and end) between the two other end)'s AnAnonymousDeveloper 77 — 6y
0
how about, how can i save player max health?, i need for maxhealth cause for defense stats save, please help Minecrafter09031031 16 — 4y
Ad

Answer this question