How would I go about saving an upgrade shop?

Hi. So I have made this speed upgrade shop and it’s all going pretty well, but how would I make it save? As of now, it removes coins from the player and boosts their speed. How can I make their speed save? Here is the script and I just wouldn’t know where to start with this so thanks to anyone who even attempts to help!

local player = game.Players.LocalPlayer
local nowalkspeed = game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed
script.Parent.Text = "30 Coins"
local AmountLeft = script.Parent.Parent.AmountOfUpgrades.AmountLeft
local AmountUpgraded = script.Parent.Parent.AmountOfUpgrades.AmountUpgraded
script.Parent.Parent.AmountOfUpgrades.Text = AmountUpgraded.Value.." / "..AmountLeft.Value
script.Parent.MouseButton1Click:Connect(function()
	print("pressed upgrade")
	print(script.Parent.Parent.AmountOfUpgrades.AmountUpgraded.Value)
	script.Parent.Parent.AmountOfUpgrades.Text = AmountUpgraded.Value.." / "..AmountLeft.Value
	if AmountUpgraded.Value == 0 then
		if player:WaitForChild("leaderstats").Coins.Value >= 30 then
			player:WaitForChild("leaderstats").Coins.Value -= 30
			AmountUpgraded.Value += 1
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = nowalkspeed + 1
			script.Parent.Text = "50 Coins"
			print(game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed)
		else
			print(player.Name.. " doesn't have enough coins to upgrade")
			script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863)
			script.Parent.Text = "Not Enough Coins!!"
			task.wait(1)
			script.Parent.BackgroundColor3 = Color3.new(1, 0.85098, 0)
			script.Parent.Text = "50 Coins"
		end
	elseif AmountUpgraded.Value == 1 then
		if player:WaitForChild("leaderstats").Coins.Value >= 30 then
			player:WaitForChild("leaderstats").Coins.Value -= 30
			AmountUpgraded.Value += 1
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = nowalkspeed + 3
			script.Parent.Text = "50 Coins"
			print(game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed)
		else
			print(player.Name.. " doesn't have enough coins to upgrade")
			script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863)
			script.Parent.Text = "Not Enough Coins!!"
			task.wait(1)
			script.Parent.BackgroundColor3 = Color3.new(1, 0.85098, 0)
			script.Parent.Text = "50 Coins"
		end
	elseif AmountUpgraded.Value == 2 then
		if player:WaitForChild("leaderstats").Coins.Value >= 30 then
			player:WaitForChild("leaderstats").Coins.Value -= 30
			AmountUpgraded.Value += 1
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = nowalkspeed + 5
			script.Parent.Text = "50 Coins"
			print(game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed)
		else
			print(player.Name.. " doesn't have enough coins to upgrade")
			script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863)
			script.Parent.Text = "Not Enough Coins!!"
			task.wait(1)
			script.Parent.BackgroundColor3 = Color3.new(1, 0.85098, 0)
			script.Parent.Text = "50 Coins"
		end
	elseif AmountUpgraded.Value == 3 then
		if player:WaitForChild("leaderstats").Coins.Value >= 50 then
			player:WaitForChild("leaderstats").Coins.Value -= 50
			AmountUpgraded.Value += 1
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = nowalkspeed + 10
			script.Parent.Text = "100 Coins"
			print(game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed)
		else
			print(player.Name.. " doesn't have enough coins to upgrade")
			script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863)
			script.Parent.Text = "Not Enough Coins!!"
			task.wait(1)
			script.Parent.BackgroundColor3 = Color3.new(1, 0.85098, 0)
			script.Parent.Text = "100 Coins"
		end
	elseif AmountUpgraded.Value == 4 then
		if player:WaitForChild("leaderstats").Coins.Value >= 100 then
			player:WaitForChild("leaderstats").Coins.Value -= 100
			AmountUpgraded.Value += 1
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = nowalkspeed + 15
			script.Parent.Text = "150 Coins"
			print(game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed)
		else
			print(player.Name.. " doesn't have enough coins to upgrade")
			script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863)
			script.Parent.Text = "Not Enough Coins!!"
			task.wait(1)
			script.Parent.BackgroundColor3 = Color3.new(1, 0.85098, 0)
			script.Parent.Text = "150 Coins"
		end
	elseif AmountUpgraded.Value == 5 then
		if player:WaitForChild("leaderstats").Coins.Value >= 150 then
			player:WaitForChild("leaderstats").Coins.Value -= 150
			AmountUpgraded.Value += 1
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = nowalkspeed + 20
			script.Parent.Text = "200 Coins"
			AmountUpgraded.Value = AmountUpgraded.Value + 1
			print(game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed)
		else
			print(player.Name.. " doesn't have enough coins to upgrade")
			script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863)
			script.Parent.Text = "Not Enough Coins!!"
			task.wait(1)
			script.Parent.BackgroundColor3 = Color3.new(1, 0.85098, 0)
			script.Parent.Text = "200 Coins"
		end
	elseif AmountUpgraded.Value == 6 then
		if player:WaitForChild("leaderstats").Coins.Value >= 200 then
			player:WaitForChild("leaderstats").Coins.Value -= 200
			AmountUpgraded.Value += 1
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = nowalkspeed + 25
			script.Parent.Text = "250 Coins"
			AmountUpgraded.Value = AmountUpgraded.Value + 1
			print(game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed)
		else
			print(player.Name.. " doesn't have enough coins to upgrade")
			script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863)
			script.Parent.Text = "Not Enough Coins!!"
			task.wait(1)
			script.Parent.BackgroundColor3 = Color3.new(1, 0.85098, 0)
			script.Parent.Text = "250 Coins"
		end
	elseif AmountUpgraded.Value == 7 then
		if player:WaitForChild("leaderstats").Coins.Value >= 250 then
			player:WaitForChild("leaderstats").Coins.Value -= 250
			AmountUpgraded.Value += 1
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = nowalkspeed + 30
			script.Parent.Text = "300 Coins"
			print(game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed)
		else
			print(player.Name.. " doesn't have enough coins to upgrade")
			script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863)
			script.Parent.Text = "Not Enough Coins!!"
			task.wait(1)
			script.Parent.BackgroundColor3 = Color3.new(1, 0.85098, 0)
			script.Parent.Text = "300 Coins"
		end
	elseif AmountUpgraded.Value == 8 then
		if player:WaitForChild("leaderstats").Coins.Value >= 300 then
			player:WaitForChild("leaderstats").Coins.Value -= 300
			AmountUpgraded.Value += 1
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = nowalkspeed + 35
			script.Parent.Text = "350 Coins"
			print(game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed)
		else
			print(player.Name.. " doesn't have enough coins to upgrade")
			script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863)
			script.Parent.Text = "Not Enough Coins!!"
			task.wait(1)
			script.Parent.BackgroundColor3 = Color3.new(1, 0.85098, 0)
			script.Parent.Text = "350 Coins"
		end
	elseif AmountUpgraded.Value == 9 then
		if player:WaitForChild("leaderstats").Coins.Value >= 350 then
			player:WaitForChild("leaderstats").Coins.Value -= 350
			AmountUpgraded.Value += 1
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = nowalkspeed + 40
			script.Parent.Text = "400 Coins"
			print(game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed)
		else
			print(player.Name.. " doesn't have enough coins to upgrade")
			script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863)
			script.Parent.Text = "Not Enough Coins!!"
			task.wait(1)
			script.Parent.BackgroundColor3 = Color3.new(1, 0.85098, 0)
			script.Parent.Text = "400 Coins"
		end
	elseif AmountUpgraded.Value >= 10 then
		if player:WaitForChild("leaderstats").Coins.Value >= 400 then
			player:WaitForChild("leaderstats").Coins.Value -= 400
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = nowalkspeed + 50
			script.Parent.Text = "MAXED"
			print(game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed)
		else
			print(player.Name.. " doesn't have enough coins to upgrade")
			script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863)
			script.Parent.Text = "Not Enough Coins!!"
			task.wait(1)
			script.Parent.BackgroundColor3 = Color3.new(1, 0.85098, 0)
			script.Parent.Text = "MAXED"
		end
	end
end)
4 Likes

So you mean when they leave the game it is not saving the speed? if so then your answer lies in DataStores, I can try and help if this is what you mean but i only recently started learning them so first do some reading up on them if you think they are what you need.

1 Like

Yeah, it has something to do with datastores. I’m not experienced with scripting, especially when it comes to datastores, so I would appreciate some help with this. Do you know how I can implement datastores into this script to save AmountUpgraded and the player’s speed?

Yes, the coins save. Thanks for your help :slight_smile:

alr so im working on making something rn, is speed saved in the leaderstats??
if not you should probably add it to leaderstats like you did with coins, as it makes it a lot easier

1 Like
local DS = game:GetService("DataStoreService")
local PS = game:GetService("Players")

local SpeedDS = DS:GetDataStore("Speed")


--Loads Speed Data When Player joins
game.Players.PlayerAdded:Connect(function(player)	
	local success, errormessage = pcall(function() --DataStores are always best to be wrapped in a pcall in case of errors
		local Data = SpeedDS:GetAsync(player.UserId)
		if Data then --If there is data then it is loaded
			player.leaderstats.Speed.Value = Data
		else --Otherwise they are a new player and we dont need to load anything
			print("no data to be loaded")
		end
	end)
		
	if success then
		print("loading succeeded")
	else
		warn(errormessage)
	end
end)

--Save Speed Data When Player Leaves
game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function() 
		SpeedDS:SetAsync(player.UserId, player.leaderstats.Speed.Value)--Second value is what we are actually saving, first is the key which helps us find it again
	end)
		
	if success then
		print("saving succeeded")
	else
		warn(errormessage)
	end
end)

That should work, put this in a regular script in ServerScriptService and add a leaderstat value called speed in the same place where whatever script creates the “Coins” leaderstat is created, give it the value of whatever you want the speed of a brand new player to be

Put this bit of code in a new local script if you want, or put it on line 2 of the one you showed, underneath the player variable, and definetly above the nowalkspeed variable

local SpeedStats = player:WaitForChild("leaderstats").Speed.Value
game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = SpeedStats 

what that does is reads what the leaderstats value for speed is for that player, and sets it to the “SpeedStats” variable, then sets the players walkspeed to the SpeedStats variable, you could do it without the SpeedStats variable, but its easier to read like this, and the code doesnt end up on a really long line.

If you have any errors, tell me and ill try my best

1 Like

So did this script help you at all?

Sorry for the delayed response. I have tested the script and have found that it doesn’t save the leaderstat “walkspeed”. I also need it to save the buttons in the shop so that players can’t purchase over the walkspeed limit / buy walkspeed that they’ve already bought. Thanks for trying to help. :slight_smile:

I didnt get it to save “walkspeed” but instead “Speed” try changing that and what do you mean by walkspeed limit?

1 Like