Hi everyone, I have made a DataStore for my game which obviously saves in game values, but somehow everything saves except maxJump and maxSpeed, which are worth 0 when you spawn… Did I o anything wrong? I tried updating the value Server Side once in a game and that didn’t save it, which means it is more than likely a bug in the DataStore script.
local ServerStorage = game:GetService("ServerStorage")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = leaderstats
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = leaderstats
local Activations = Instance.new("IntValue")
Activations.Name = "Activations"
Activations.Parent = leaderstats
local Deaths = Instance.new("IntValue")
Deaths.Name = "Deaths"
Deaths.Parent = leaderstats
local Upgrades = Instance.new("Folder")
Upgrades.Name = "Upgrades"
Upgrades.Parent = player
local SpeedLevel = Instance.new("IntValue")
SpeedLevel.Name = "SpeedLevel"
SpeedLevel.Parent = Upgrades
local JumpLevel = Instance.new("IntValue")
JumpLevel.Name = "JumpLevel"
JumpLevel.Parent = Upgrades
local maxSpeed = Instance.new("IntValue")
maxSpeed.Name = "maxSpeed"
maxSpeed.Parent = player
local maxJump = Instance.new("IntValue")
maxJump.Name = "maxJump"
maxJump.Parent = player
-->> Data: Begining
local playerUserId = "Player_"..player.UserId
-->>Data: Load Data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
if data then
Points.Value = data.Points
Wins.Value = data.Wins
Activations.Value = data.Activations
Deaths.Value = data.Deaths
SpeedLevel.Value = data.SpeedLevel
JumpLevel.Value = data.JumpLevel
maxSpeed.Value = data.maxSpeed
maxJump.Value = data.maxJump
end
end
end)
-->> Data: Saving Stats
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = {
Points = player.leaderstats.Points.Value;
Wins = player.leaderstats.Wins.Value;
Activations = player.leaderstats.Activations.Value;
Deaths = player.leaderstats.Deaths.Value;
SpeedLevel = player.Upgrades.SpeedLevel.Value;
JumpLevel = player.Upgrades.JumpLevel.Value;
maxSpeed = player.maxSpeed.Value;
maxJump = player.maxJump.Value;
}
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data succefully saved!")
else
print("Data saving error")
warn(errormessage)
end
end)
The only thing, that comes into my head is the fact, when you leave Roblox Studio test, the data doesn’t save too often. Have you tried it in regular published game, where you are sure, the values maxJump and maxSpeed are on different value, then 1?
Yes, but now none of my Data saves somehow… I think maxJump and maxSpeed are breaking DataStores, just like when I was trying to save WalkSpeed and JumpPower… The thing is, I hve no clue why since they are exactly the same as other values… Do you think it might be when I update them in other scripts it doesn’t work?
If this is storing a Humanoids walkspeed/jump power, use NumberValues instead of IntValues, Im fairly sure that WalkSpeed and JumpPower can have Decimals, also it might be good to show how these values are being Updated before PlayerRemoving happens as they may not be properly updating the Value within player. HOWEVER! if this is a form of an upgrade for example, if their value speed is 20 then it allows that player to go at that speed, the issue is probably not with your datastore but with script that edits the value during the gameplay. if this doesn’t work then I would check to see why similar values are saving and spot the differences.
Ah, its what I was looking into right now. I have suspisions on TWO scripts, one that uses the maxSpeed to verify if a player can use a number they enter (its a custom speed textBox, and maxSpeed verifies if the player can have that maxSpeed or if he/she is over that speed). The other one is upon receiving a RemoteEvent that is received when a player upgraded their speed. I don’t know if this helps, but I tested the game with the first script disabled and the DataStore still DIDN’T work. Here are the scripts:
First script:
local Players = game.Players
local player = Players.LocalPlayer
local textBox = script.Parent
local maxSpeed = player.maxSpeed.Value
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
local function OnFocusLost(enterPressed)
if enterPressed then
local speed = textBox.Text
if speed <= 16 then
player.Character.Humanoid.WalkSpeed = 16
elseif speed >= 16 and speed <= maxSpeed.Value then
player.Character.Humanoid.WalkSpeed = speed
elseif speed >= maxSpeed.Value then
player.Character.Humanoid.WalkSpeed = maxSpeed.Value
else
textBox.Text = ""
textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
end
else
textBox.Text = ""
textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
end
end
textBox.FocusLost:Connect(OnFocusLost)
while true do
textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
wait(1)
end
Another important detail I think is that the first script is LOCAL, inside a GUI, whereas the second script is REGULAR and is located in SERVER SCRIPT SERVICE
You should check to see if your values are changing whilst in game, it might be that they aren’t actually changing. Either that or the If statement isn’t actually being ran. I would use the in-game command console to award yourself a max speed value, and then try leaving and rejoining the game to see if it saves. Also are you checking if it saves in Studio? If you are then that’s why it wont save as your data store won’t save your data in studio.
One question at a time. The values ARE indeed changing in game. The if statement doesn’t change the values if you’re talking abt the one in the first script. I also forgot to mention that I still have issues with this script and it doesn’t work, although I’m confident that’s not related to DataSaving. Lastly I haven’t tried playing in a real Server, but everytime I tried it in Studio before, it used to save my Data.
That’s odd, your data shouldn’t save in Studio, It may appear to save as you had already been in-game before and had data saved? Try playing in a server for your place, and it might just be that it’s not saving because you’re using studio which doesn’t save your data (from the given DataStore method)
local Players = game.Players
local player = Players.LocalPlayer
local textBox = script.Parent
local maxSpeed = player.maxSpeed.Value
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
local function OnFocusLost(enterPressed)
if enterPressed then
local speed = textBox.Text
if speed <= 16 then
player.Character.Humanoid.WalkSpeed = 16
elseif speed >= 16 and speed <= maxSpeed.Value then
player.Character.Humanoid.WalkSpeed = speed
elseif speed >= maxSpeed.Value then
player.Character.Humanoid.WalkSpeed = maxSpeed.Value
else
textBox.Text = ""
textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
end
else
textBox.Text = ""
textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
end
end
textBox.FocusLost:Connect(OnFocusLost)
while true do
textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
wait(1)
end
The output states that " Players.MrDaBigBoss.PlayerGui.Menu.Frame.CustomSpeed.LocalScript:34: attempt to index number with ‘Value’ " (line 34) and when I enter a custom value it says “Players.MrDaBigBoss.PlayerGui.Menu.Frame.CustomSpeed.LocalScript:15: attempt to compare string and number” (line 15)
Edit: Hold up… sry… lemme remove those…
2nd Edit: Ok so I modified my script and took of the .Value to the maxSpeed Variable, but that still outputed the 2nd error I had. (See above)
At the top of your script you have defined maxSpeed as its Value, you haven’t defined it as just maxSpeed. Instead of it being
You should make it only be player.maxSpeed otherwise you are only storing whatever your maxspeed value is at that point of time,
The issue is because you have defined maxSpeed as its value, instead of it being maxSpeed itself. (Essentially you trying to get a numbers value which isn’t possible as a number is a value)
speed would be a string value since you are defining it as the TextBox “Text”. you would want to instead convert this from string to integer so you can compare the values.
Hey, sorry this is not over, but somehow it still says that I’m trying to compare string and number on line 16:
local Players = game.Players
local player = Players.LocalPlayer
local textBox = script.Parent
local maxSpeed = player.maxSpeed
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
local function OnFocusLost(enterPressed)
if enterPressed then
local speed = textBox.Text
if tonumber(speed) then
if speed <= 16 then -- Line 16
player.Character.Humanoid.WalkSpeed = 16
elseif speed >= 16 and speed <= maxSpeed.Value then
player.Character.Humanoid.WalkSpeed = speed
elseif speed >= maxSpeed.Value then
player.Character.Humanoid.WalkSpeed = maxSpeed.Value
end
else
textBox.Text = ""
textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
end
else
textBox.Text = ""
textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
end
end
textBox.FocusLost:Connect(OnFocusLost)
while true do
textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
wait(1)
end
you are doing “if tonumber(speed)” you are only changing speed for that if statement. This doesn’t mean it will replicated to every other callup for that variable.