DataStore Working except for 2 values

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?

1 Like

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

Second script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.SpeedUpgraded.OnServerEvent:Connect(function(player)
	if player.Upgrades.SpeedLevel.Value == 0 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 100
		player.Upgrades.SpeedLevel.Value = 1
		player.Character.Humanoid.WalkSpeed = 20
		player.maxSpeed.Value = 20
	elseif player.Upgrades.SpeedLevel.Value == 1 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 200
		player.Upgrades.SpeedLevel.Value = 2
		player.Character.Humanoid.WalkSpeed = 24
		player.maxSpeed.Value = 24
	elseif player.Upgrades.SpeedLevel.Value == 2 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 300
		player.Upgrades.SpeedLevel.Value = 3
		player.Character.Humanoid.WalkSpeed = 28
		player.maxSpeed.Value = 28
	elseif player.Upgrades.SpeedLevel.Value == 3 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 400
		player.Upgrades.SpeedLevel.Value = 4
		player.Character.Humanoid.WalkSpeed = 32
		player.maxSpeed.Value = 32
	elseif player.Upgrades.SpeedLevel.Value == 4 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 500
		player.Upgrades.SpeedLevel.Value = 5
		player.Character.Humanoid.WalkSpeed = 36
		player.maxSpeed.Value = 36
	elseif player.Upgrades.SpeedLevel.Value == 5 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 600
		player.Upgrades.SpeedLevel.Value = 6
		player.Character.Humanoid.WalkSpeed = 40
		player.maxSpeed.Value = 40
	elseif player.Upgrades.SpeedLevel.Value == 6 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 700
		player.Upgrades.SpeedLevel.Value = 7
		player.Character.Humanoid.WalkSpeed = 45
		player.maxSpeed.Value = 45
	elseif player.Upgrades.SpeedLevel.Value == 7 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 800
		player.Upgrades.SpeedLevel.Value = 8
		player.Character.Humanoid.WalkSpeed = 50
		player.maxSpeed.Value = 50
	elseif player.Upgrades.SpeedLevel.Value == 8 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 900
		player.Upgrades.SpeedLevel.Value = 9
		player.Character.Humanoid.WalkSpeed = 55
		player.maxSpeed.Value = 55
	elseif player.Upgrades.SpeedLevel.Value == 9 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 1000
		player.Upgrades.SpeedLevel.Value = 10
		player.Character.Humanoid.WalkSpeed = 60
		player.maxSpeed.Value = 55
	end
end)

ReplicatedStorage.JumpUpgraded.OnServerEvent:Connect(function(player)
	if player.Upgrades.JumpLevel.Value == 0 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 150
		player.Upgrades.JumpLevel.Value = 1
		player.Character.Humanoid.JumpPower = 60
		player.maxJump.Value = 60
	elseif player.Upgrades.JumpLevel.Value == 1 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 300
		player.Upgrades.JumpLevel.Value = 2
		player.Character.Humanoid.JumpPower = 70
		player.maxJump.Value = 70
	elseif player.Upgrades.JumpLevel.Value == 2 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 450
		player.Upgrades.JumpLevel.Value = 3
		player.Character.Humanoid.JumpPower = 80
		player.maxJump.Value = 80
	elseif player.Upgrades.JumpLevel.Value == 3 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 600
		player.Upgrades.JumpLevel.Value = 4
		player.Character.Humanoid.JumpPower = 90
		player.maxJump.Value = 90
	elseif player.Upgrades.JumpLevel.Value == 4 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 750
		player.Upgrades.JumpLevel.Value = 5
		player.Character.Humanoid.JumpPower = 100
		player.maxJump.Value = 100
	elseif player.Upgrades.JumpLevel.Value == 5 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 900
		player.Upgrades.JumpLevel.Value = 6
		player.Character.Humanoid.JumpPower = 110
		player.maxJump.Value = 110
	elseif player.Upgrades.JumpLevel.Value == 6 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 1050
		player.Upgrades.JumpLevel.Value = 7
		player.Character.Humanoid.JumpPower = 125
		player.maxJump.Value = 125
	elseif player.Upgrades.JumpLevel.Value == 7 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 1200
		player.Upgrades.JumpLevel.Value = 8
		player.Character.Humanoid.JumpPower = 150
		player.maxJump.Value = 150
	elseif player.Upgrades.JumpLevel.Value == 8 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 1350
		player.Upgrades.JumpLevel.Value = 9
		player.Character.Humanoid.JumpPower = 175
		player.maxJump.Value = 175
	elseif player.Upgrades.JumpLevel.Value == 9 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 1500
		player.Upgrades.JumpLevel.Value = 10
		player.Character.Humanoid.JumpPower = 200
		player.maxJump.Value = 200
	end
end)
1 Like

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

1 Like

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)

Wow… I just figured out DataStores work in a real server, AND that the maxJump and maxSpeed save. Thank you so much for your help.

1 Like

Now I have another problem with my 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

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)

1 Like

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)

1 Like

Yep I changed that, although now the 2nd error still need a fix, but I don’t understand how I’m trying to compare string and number???..

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.

But how do I do that, TextBox cna only have string values… I also have an else that makes the TextBox not do anything if they enter something else.

1 Like

Read this article which shows you how to manipulate a string.
https://developer.roblox.com/en-us/articles/String

2 Likes

I saw this on Scripting Helpers, would you think it would work?

if tonumber(TextBox.Text) then
     -- code
else
    TextBox.Text = "Enter a number!!!!"
end
1 Like

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
1 Like

This is the FINAL glitch, so I need help please…

1 Like

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.