Help with Datastore

I’m having issues making a working data store for my current leaderstats script, and I was wondering if anyone on here with more knowledge could help me achieve my goal.

My leaderstats script without any data store

local RebirthEvent = game.ReplicatedStorage:WaitForChild("RebirthEvent")
local Players = game:GetService('Players')

game.Players.PlayerAdded:Connect(function(player)

	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	

	local Speed = Instance.new("IntValue")
	Speed.Name = "Speed"
	Speed.Value = 0
	Speed.Parent = leaderstats
	
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Value = 0
	Rebirths.Parent = leaderstats
	
	
	local Multiplier = Instance.new("NumberValue", player)
	Multiplier.Name = "Multiplier"
	Multiplier.Value = 1
	
	
	local RequiredPoints = Instance.new("IntValue", player)
	RequiredPoints.Name = "RequiredPoints"
	RequiredPoints.Value = 7000
	
end)


---------------------------------------------------------------------------------------------------------------------
-- Other Stuff

RebirthEvent.OnServerEvent:Connect(function(Player)
	
	local leaderstats = Player:WaitForChild("leaderstats")
	local Multiplier = Player:WaitForChild("Multiplier")
	
	leaderstats.Rebirths.Value += 1
	
	Multiplier.Value += 0.25
end)





game.Players.ChildAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	local leaderstats = player:WaitForChild("leaderstats")
	local humanoid = player.Character:WaitForChild("Humanoid")
	if leaderstats.Speed.Value == 0 then
	humanoid.WalkSpeed = 0
	end
	end)


local RemovteEvent = game.ReplicatedStorage.CodeEvent


RemovteEvent.OnServerEvent:Connect(function(Player, Reward, Code)
	if Player:FindFirstChild(Code) == nil then
		local Redeemed = Instance.new("BoolValue",Player)
		Redeemed.Name = Code
		Redeemed.Value = false

		if Redeemed.Value == false then
			Player.leaderstats.Speed.Value += Reward
			Redeemed.Value = true
		end
	end
end)```
1 Like

What’s the problem you are facing?

1 Like

Hello, What is the issues your having with it?

Sorry for being a bit late, I can’t really set up my stuff, I’ve tried many different data store youtube tutorial but non seem to work

Something like this

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerValues")
local RebirthEvent = game.ReplicatedStorage:WaitForChild("RebirthEvent")
local Players = game:GetService('Players')

game.Players.PlayerAdded:Connect(function(player)


	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player



	local Speed = Instance.new("IntValue", leaderstats)
	Speed.Name = "Speed"
	Speed.Value = DataStore:GetAsync(player.UserId, Speed.Value) or 0

	local Rebirths = Instance.new("IntValue", leaderstats)
	Rebirths.Name = "Rebirths"
	Rebirths.Value = DataStore:GetAsync(player.UserId, Rebirths.Value)

	local Multiplier = Instance.new("NumberValue", player)
	Multiplier.Name = "Multiplier"
	Multiplier.Value = DataStore:GetAsync(player.UserId, Multiplier.Value) or 1


	local RequiredPoints = Instance.new("IntValue", player)
	RequiredPoints.Name = "RequiredPoints"
	RequiredPoints.Value = Multiplier.Value*7000
	Speed:GetPropertyChangedSignal("Value"):Connect(function()
		DataStore:UpdateAsync(player.UserId, Speed.Value)
	end)
	Rebirths:GetPropertyChangedSignal("Value"):Connect(function()
		DataStore:UpdateAsync(player.UserId, Rebirths.Value)
	end)
	Multiplier:GetPropertyChangedSignal("Value"):Connect(function()
		DataStore:UpdateAsync(player.UserId, Multiplier.Value)	
	end)

game.Players.PlayerRemoving:Connect(function(player)
		DataStore:SetAsync(player.UserId, Speed.Value)
		DataStore:SetAsync(player.UserId, Rebirths.Value)
		DataStore:SetAsync(player.UserId, Multiplier.Value)
end)	
end)


---------------------------------------------------------------------------------------------------------------------
-- Other Stuff

RebirthEvent.OnServerEvent:Connect(function(Player)

	local leaderstats = Player:WaitForChild("leaderstats")
	local Multiplier = Player:WaitForChild("Multiplier")

	leaderstats.Rebirths.Value += 1

	Multiplier.Value += 0.25
end)





game.Players.ChildAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	local leaderstats = player:WaitForChild("leaderstats")
	local humanoid = player.Character:WaitForChild("Humanoid")
	
		humanoid.WalkSpeed = leaderstats.Speed.Value

end)


local RemovteEvent = game.ReplicatedStorage.CodeEvent


RemovteEvent.OnServerEvent:Connect(function(Player, Reward, Code)
	if Player:FindFirstChild(Code) == nil then
		local Redeemed = Instance.new("BoolValue",Player)
		Redeemed.Name = Code
		Redeemed.Value = false

		if Redeemed.Value == false then
			Player.leaderstats.Speed.Value += Reward
			Redeemed.Value = true
		end
	end
end)


2 Likes

Hey, this works with everything expect the speed value, for some reason the speed.value just resets to 1 everytime I load back in

You might want something like this:

local RebirthEvent = game.ReplicatedStorage:WaitForChild("RebirthEvent")
local DatastoreService = game:GetService("DataStoreService")
local DataStore = DatastoreService:GetDataStore("YOUR_DATASTORE_NAME_HERE")
local Players = game:GetService('Players')

game.Players.PlayerAdded:Connect(function(player)

	local PlayerData;
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	

	local Speed = Instance.new("IntValue")
	Speed.Name = "Speed"
	Speed.Value = 0
	Speed.Parent = leaderstats
	
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Value = 0
	Rebirths.Parent = leaderstats
	
	
	local Multiplier = Instance.new("NumberValue", player)
	Multiplier.Name = "Multiplier"
	Multiplier.Value = 1
	
	
	local RequiredPoints = Instance.new("IntValue", player)
	RequiredPoints.Name = "RequiredPoints"
	RequiredPoints.Value = 7000
	
	local success,_ = pcall(function()
	PlayerData = DataStore:GetAsync(player.UserId) -- It is recomended to use UserId instead of player's username incase they change their username in the future
	end)
	if not success then
		repeat
			warn("Data failed to load! Retrying...")
			wait(3)
			success,_ = pcall(function()
	          PlayerData = DataStore:GetAsync(player.UserId) -- It is recomended to use UserId instead of player's username incase they change their username in the future
	         end)
		until success
	end
	warn("Data Loaded!")
	if PlayerData == nil then
		DataStore:SetAsync(player.UserId,{
			["Speed"] = Speed.Value;
			["Rebirths"] = Rebirths.Value;
			["Multiplier"] = Multiplier.Value;			
		})
	else
		Speed.Value = PlayerData["Speed"]
		Rebirths.Value = PlayerData["Rebirths"]
		Multiplier.Value = PlayerData["Multiplier"]
	end
	leaderstats.Parent = player
end)

game.Players.PlayerRemoving:Connect(function(player)
	if not player:FindFirstChild("leaderstats") then return end
	local success,_ = pcall(function()
	DataStore:SetAsync(player.UserId,{
			["Speed"] = player:FindFirstChild("leaderstats").Speed.Value;
			["Rebirths"] = player:FindFirstChild("leaderstats").Rebirths.Value;
			["Multiplier"] = player.Multiplier.Value;			
		})
	end)
	if not success then
		repeat
			warn("Data failed to save! Retrying...")
			wait(3)
			success,_ = pcall(function()
	         DataStore:SetAsync(player.UserId,{
			["Speed"] = player:FindFirstChild("leaderstats").Speed.Value;
			["Rebirths"] = player:FindFirstChild("leaderstats").Rebirths.Value;
			["Multiplier"] = player.Multiplier.Value;			
		     })
	         end)
		until success
	end
end)

---------------------------------------------------------------------------------------------------------------------
-- Other Stuff

RebirthEvent.OnServerEvent:Connect(function(Player)
	
	local leaderstats = Player:WaitForChild("leaderstats")
	local Multiplier = Player:WaitForChild("Multiplier")
	
	leaderstats.Rebirths.Value += 1
	
	Multiplier.Value += 0.25
end)





game.Players.ChildAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	local leaderstats = player:WaitForChild("leaderstats")
	local humanoid = player.Character:WaitForChild("Humanoid")
	if leaderstats.Speed.Value == 0 then
	humanoid.WalkSpeed = 0
	end
	end)


local RemovteEvent = game.ReplicatedStorage.CodeEvent


RemovteEvent.OnServerEvent:Connect(function(Player, Reward, Code)
	if Player:FindFirstChild(Code) == nil then
		local Redeemed = Instance.new("BoolValue",Player)
		Redeemed.Name = Code
		Redeemed.Value = false

		if Redeemed.Value == false then
			Player.leaderstats.Speed.Value += Reward
			Redeemed.Value = true
		end
	end
end)
2 Likes

I wouldn’t recommend using DataStore like that and especially DataStore:UpdateAsync is discontinued by Roblox so it is not recommended to use it for new projects

1 Like

I have been using it, and i have no issues with it, it kind of prevents Data Queues

1 Like

This unfortunately has the same issue

Do you have any other script that changes the Speed value to 1?

1 Like

Let me check thru all of them!

I have this script that gives the player speed per click, it also changes the players walkspeed based on how much speed they have

local player = game.Players.LocalPlayer
local Speed = player:WaitForChild("leaderstats"):WaitForChild("Speed")
local sound = script["New pop Clikc"]
local popup = game.Players.LocalPlayer.PlayerGui:WaitForChild("StarterScreen"):WaitForChild("Frame")
local leaderstats = player:WaitForChild("leaderstats")
local Multiplier = player:WaitForChild("Multiplier")


debounce = true


game:GetService("UserInputService").InputBegan:Connect(function(input, engine_processed)
if engine_processed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if debounce == true then
			debounce = false
			if Multiplier.Value > 0 then
				Speed.Value += 1 * Multiplier.Value 
			else
				Speed.Value += 1
			end
			
			local Char = player.Character or player.CharacterAdded:Wait()
			local Hum = Char.Humanoid
			Hum.WalkSpeed = Speed.Value / 17
			if Speed.Value >= 1000 then
				Hum.WalkSpeed = Speed.Value / 25
			end
			if Speed.Value >= 2000 then
				Hum.WalkSpeed = Speed.Value / 40
			end
			if Speed.Value >= 3000 then
				Hum.WalkSpeed = Speed.Value / 50
			end
			if Speed.Value >= 5000 then
				Hum.WalkSpeed = Speed.Value / 60
			end
			if Speed.Value >= 7000 then
				Hum.WalkSpeed = Speed.Value / 70
			end
			if Speed.Value >= 10000 then
				Hum.WalkSpeed = Speed.Value / 90
			end
			if Speed.Value >= 15000 then
				Hum.WalkSpeed = Speed.Value / 120
			end
			if Speed.Value >= 20000 then
				Hum.WalkSpeed = Speed.Value / 150
			end
			if Speed.Value >= 30000 then
				Hum.WalkSpeed = Speed.Value / 190
			end
			if Speed.Value >= 50000 then
				Hum.WalkSpeed = Speed.Value / 230
			end
			if Speed.Value >= 70000 then
				Hum.WalkSpeed = Speed.Value / 270
			end
			if Speed.Value >= 90000 then
				Hum.WalkSpeed = Speed.Value / 350
			end
			if Speed.Value >= 100000 then
				Hum.WalkSpeed = Speed.Value / 450
			end
			sound:Play()
			wait(0.158)
			debounce = true
		end
	end
end)
		



Is this a local script or a server script?

1 Like

It is a local script under starterplayerscript

I see what’s the problem. So basically you actually can’t change the values of leaderstats if you are using a local script. You might wanna add a remote function in order to communicate with the server scripts.

local script:

local player = game.Players.LocalPlayer
local Speed = player:WaitForChild("leaderstats"):WaitForChild("Speed")
local sound = script["New pop Clikc"]
local popup = game.Players.LocalPlayer.PlayerGui:WaitForChild("StarterScreen"):WaitForChild("Frame")
local leaderstats = player:WaitForChild("leaderstats")
local Multiplier = player:WaitForChild("Multiplier")
local SpeedIncrease = game.ReplicatedStorage:WaitForChild("SpeedIncrease")


debounce = true


game:GetService("UserInputService").InputBegan:Connect(function(input, engine_processed)
if engine_processed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if debounce == true then
			debounce = false
			if Multiplier.Value > 0 then
				SpeedIncrease:FireServer((1 * Multiplier.Value))
			else
				SpeedIncrease:FireServer(1)
			end
			
			local Char = player.Character or player.CharacterAdded:Wait()
			local Hum = Char.Humanoid
			Hum.WalkSpeed = Speed.Value / 17
			if Speed.Value >= 1000 then
				Hum.WalkSpeed = Speed.Value / 25
			end
			if Speed.Value >= 2000 then
				Hum.WalkSpeed = Speed.Value / 40
			end
			if Speed.Value >= 3000 then
				Hum.WalkSpeed = Speed.Value / 50
			end
			if Speed.Value >= 5000 then
				Hum.WalkSpeed = Speed.Value / 60
			end
			if Speed.Value >= 7000 then
				Hum.WalkSpeed = Speed.Value / 70
			end
			if Speed.Value >= 10000 then
				Hum.WalkSpeed = Speed.Value / 90
			end
			if Speed.Value >= 15000 then
				Hum.WalkSpeed = Speed.Value / 120
			end
			if Speed.Value >= 20000 then
				Hum.WalkSpeed = Speed.Value / 150
			end
			if Speed.Value >= 30000 then
				Hum.WalkSpeed = Speed.Value / 190
			end
			if Speed.Value >= 50000 then
				Hum.WalkSpeed = Speed.Value / 230
			end
			if Speed.Value >= 70000 then
				Hum.WalkSpeed = Speed.Value / 270
			end
			if Speed.Value >= 90000 then
				Hum.WalkSpeed = Speed.Value / 350
			end
			if Speed.Value >= 100000 then
				Hum.WalkSpeed = Speed.Value / 450
			end
			sound:Play()
			wait(0.158)
			debounce = true
		end
	end
end)

Server script:

game.ReplicatedStorage.SpeedIncrease.OnServerEvent:Connect(function(Player,Value)
	repeat wait() until Player:FindFirstChild("leaderstats")
	Player:FindFirstChild("leaderstats").Speed.Value += Value
end)
2 Likes

Oh wow yeah, you are truly amazing and impressive :slight_smile:

But do you know how I could save if the player already redeemed a code, so they cant just rejoin and redeem the same code again?

Server Scipt

local RemovteEvent = game.ReplicatedStorage.CodeEvent


RemovteEvent.OnServerEvent:Connect(function(Player, Reward, Code)
	if Player:FindFirstChild(Code) == nil then
		local Redeemed = Instance.new("BoolValue",Player)
		Redeemed.Name = Code
		Redeemed.Value = false

		if Redeemed.Value == false then
			Player.leaderstats.Speed.Value += Reward
			Redeemed.Value = true
		end
	end
end)

Local script

Local script
local Codes = {"Release", "Test"}
local RemoteEvent = game.ReplicatedStorage.CodeEvent
local sound = script:WaitForChild("purchase")


script.Parent.Redeem.MouseButton1Click:Connect(function()
	if script.Parent.CodeInput.Text == Codes[1] then
		sound:Play()
		RemoteEvent:FireServer(150, Codes[1])
	else
		if script.Parent.CodeInput.Text == Codes[2] then
			sound:Play()
			RemoteEvent:FireServer(3000, Codes [2])
		end
	end
end)

I used a different datastore for codes don’t forget to change the name!

local RebirthEvent = game.ReplicatedStorage:WaitForChild("RebirthEvent")
local DatastoreService = game:GetService("DataStoreService")
local DataStore = DatastoreService:GetDataStore("YOUR_DATASTORE_NAME_HERE")
local CodesDataStore = DatastoreService:GetDataStore("YOUR_DATASTORE_NAME_HERE_FOR_CODES")
local Players = game:GetService('Players')

game.Players.PlayerAdded:Connect(function(player)

	local PlayerData;
	local RedeemedCodes;
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	

	local Speed = Instance.new("IntValue")
	Speed.Name = "Speed"
	Speed.Value = 0
	Speed.Parent = leaderstats
	
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Value = 0
	Rebirths.Parent = leaderstats
	
	
	local Multiplier = Instance.new("NumberValue", player)
	Multiplier.Name = "Multiplier"
	Multiplier.Value = 1
	
	local CodesFolder = Instance.new("Folder",player)
	CodesFolder.Name = "RedeemedCodes"
	
	local RequiredPoints = Instance.new("IntValue", player)
	RequiredPoints.Name = "RequiredPoints"
	RequiredPoints.Value = 7000
	
	local success,_ = pcall(function()
		PlayerData = DataStore:GetAsync(player.UserId) -- It is recomended to use UserId instead of player's username incase they change their username in the future
		RedeemedCodes = CodesDataStore:GetAsync(player.UserId)
	end)
	if not success then
		repeat
			warn("Data failed to load! Retrying...")
			wait(3)
			success,_ = pcall(function()
				PlayerData = DataStore:GetAsync(player.UserId) -- It is recomended to use UserId instead of player's username incase they change their username in the future
				RedeemedCodes = CodesDataStore:GetAsync(player.UserId)
	         end)
		until success
	end
	warn("Data Loaded!")
	if PlayerData == nil then
		DataStore:SetAsync(player.UserId,{
			["Speed"] = Speed.Value;
			["Rebirths"] = Rebirths.Value;
			["Multiplier"] = Multiplier.Value;			
		})
	else
		Speed.Value = PlayerData["Speed"]
		Rebirths.Value = PlayerData["Rebirths"]
		Multiplier.Value = PlayerData["Multiplier"]
	end
	if RedeemedCodes ~= nil then
		for Index,Code in pairs(RedeemedCodes) do
			print(Code)
			local RedeemedCode = Instance.new("BoolValue",CodesFolder)
			RedeemedCode.Value = true
			RedeemedCode.Name = Code
		end
	end
	leaderstats.Parent = player
end)

game.Players.PlayerRemoving:Connect(function(player)
	if not player:FindFirstChild("leaderstats") then return end
	local CodesTable = {}
	for Index,Code in pairs(player.RedeemedCodes:GetChildren()) do
		print(Code)
		table.insert(CodesTable,Code.Name)
	end
	local success,_ = pcall(function()
	DataStore:SetAsync(player.UserId,{
			["Speed"] = player:FindFirstChild("leaderstats").Speed.Value;
			["Rebirths"] = player:FindFirstChild("leaderstats").Rebirths.Value;
			["Multiplier"] = player.Multiplier.Value;			
		})
	
    CodesDataStore:SetAsync(player.UserId,CodesTable)
	end)
	if not success then
		repeat
			warn("Data failed to save! Retrying...")
			wait(3)
			success,_ = pcall(function()
	         DataStore:SetAsync(player.UserId,{
			["Speed"] = player:FindFirstChild("leaderstats").Speed.Value;
			["Rebirths"] = player:FindFirstChild("leaderstats").Rebirths.Value;
			["Multiplier"] = player.Multiplier.Value;			
		     })
	         end)
		until success
	end
end)

---------------------------------------------------------------------------------------------------------------------
-- Other Stuff

RebirthEvent.OnServerEvent:Connect(function(Player)
	
	local leaderstats = Player:WaitForChild("leaderstats")
	local Multiplier = Player:WaitForChild("Multiplier")
	
	leaderstats.Rebirths.Value += 1
	
	Multiplier.Value += 0.25
end)





game.Players.ChildAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	local leaderstats = player:WaitForChild("leaderstats")
	local humanoid = player.Character:WaitForChild("Humanoid")
	if leaderstats.Speed.Value == 0 then
	humanoid.WalkSpeed = 0
	end
	end)


local RemovteEvent = game.ReplicatedStorage.CodeEvent

game.ReplicatedStorage.SpeedIncrease.OnServerEvent:Connect(function(Player,Value)
	repeat wait() until Player:FindFirstChild("leaderstats")
	Player:FindFirstChild("leaderstats").Speed.Value += Value
end)

RemovteEvent.OnServerEvent:Connect(function(Player, Reward, Code)
	for Index,RedeemedCode in pairs(Player.RedeemedCodes:GetChildren()) do
		if RedeemedCode.Name == Code then
			print("already redeemed")
			return
		end
	end
		local Redeemed = Instance.new("BoolValue",Player.RedeemedCodes)
		Redeemed.Name = Code
		Redeemed.Value = true
	Player.leaderstats.Speed.Value += Reward
end)
2 Likes

Thank you alot :pray:
but ive encounted another issue with the script, it still saves the data when a player rebirths, and it does not save the “RequiredPoints” value.

Do you have any idea on how I could fix that?

I actually didn’t know you wanted to save required points.

Take the fixed script

local RebirthEvent = game.ReplicatedStorage:WaitForChild("RebirthEvent")
local DatastoreService = game:GetService("DataStoreService")
local DataStore = DatastoreService:GetDataStore("YOUR_DATASTORE_NAME_HERE")
local CodesDataStore = DatastoreService:GetDataStore("YOUR_DATASTORE_NAME_HERE_FOR_CODES")
local Players = game:GetService('Players')

game.Players.PlayerAdded:Connect(function(player)

	local PlayerData;
	local RedeemedCodes;
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	

	local Speed = Instance.new("IntValue")
	Speed.Name = "Speed"
	Speed.Value = 0
	Speed.Parent = leaderstats
	
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Value = 0
	Rebirths.Parent = leaderstats
	
	
	local Multiplier = Instance.new("NumberValue", player)
	Multiplier.Name = "Multiplier"
	Multiplier.Value = 1
	
	local CodesFolder = Instance.new("Folder",player)
	CodesFolder.Name = "RedeemedCodes"
	
	local RequiredPoints = Instance.new("IntValue", player)
	RequiredPoints.Name = "RequiredPoints"
	RequiredPoints.Value = 7000
	
	local success,_ = pcall(function()
		PlayerData = DataStore:GetAsync(player.UserId) -- It is recomended to use UserId instead of player's username incase they change their username in the future
		RedeemedCodes = CodesDataStore:GetAsync(player.UserId)
	end)
	if not success then
		repeat
			warn("Data failed to load! Retrying...")
			wait(3)
			success,_ = pcall(function()
				PlayerData = DataStore:GetAsync(player.UserId) -- It is recomended to use UserId instead of player's username incase they change their username in the future
				RedeemedCodes = CodesDataStore:GetAsync(player.UserId)
	         end)
		until success
	end
	warn("Data Loaded!")
	if PlayerData == nil then
		DataStore:SetAsync(player.UserId,{
			["Speed"] = Speed.Value;
                        ["RequiredPoints"] = RequiredPoints.Value;
			["Rebirths"] = Rebirths.Value;
			["Multiplier"] = Multiplier.Value;			
		})
	else
		Speed.Value = PlayerData["Speed"]
		Rebirths.Value = PlayerData["Rebirths"]
		Multiplier.Value = PlayerData["Multiplier"]
	end
	if RedeemedCodes ~= nil then
		for Index,Code in pairs(RedeemedCodes) do
			print(Code)
			local RedeemedCode = Instance.new("BoolValue",CodesFolder)
			RedeemedCode.Value = true
			RedeemedCode.Name = Code
		end
	end
	leaderstats.Parent = player
end)

game.Players.PlayerRemoving:Connect(function(player)
	if not player:FindFirstChild("leaderstats") then return end
	local CodesTable = {}
	for Index,Code in pairs(player.RedeemedCodes:GetChildren()) do
		print(Code)
		table.insert(CodesTable,Code.Name)
	end
	local success,_ = pcall(function()
	DataStore:SetAsync(player.UserId,{
			["Speed"] = player:FindFirstChild("leaderstats").Speed.Value;
			["Rebirths"] = player:FindFirstChild("leaderstats").Rebirths.Value;
                        ["RequiredPoints"] =  player.RequiredPoints.Value;
			["Multiplier"] = player.Multiplier.Value;			
		})
	
    CodesDataStore:SetAsync(player.UserId,CodesTable)
	end)
	if not success then
		repeat
			warn("Data failed to save! Retrying...")
			wait(3)
			success,_ = pcall(function()
	         DataStore:SetAsync(player.UserId,{
			["Speed"] = player:FindFirstChild("leaderstats").Speed.Value;
			["Rebirths"] = player:FindFirstChild("leaderstats").Rebirths.Value;
                        ["RequiredPoints"] =  player.RequiredPoints.Value;
			["Multiplier"] = player.Multiplier.Value;			
		     })
	         end)
		until success
	end
end)

---------------------------------------------------------------------------------------------------------------------
-- Other Stuff

RebirthEvent.OnServerEvent:Connect(function(Player)
	
	local leaderstats = Player:WaitForChild("leaderstats")
	local Multiplier = Player:WaitForChild("Multiplier")
	
	leaderstats.Rebirths.Value += 1
	
	Multiplier.Value += 0.25
end)





game.Players.ChildAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	local leaderstats = player:WaitForChild("leaderstats")
	local humanoid = player.Character:WaitForChild("Humanoid")
	if leaderstats.Speed.Value == 0 then
	humanoid.WalkSpeed = 0
	end
	end)


local RemovteEvent = game.ReplicatedStorage.CodeEvent

game.ReplicatedStorage.SpeedIncrease.OnServerEvent:Connect(function(Player,Value)
	repeat wait() until Player:FindFirstChild("leaderstats")
	Player:FindFirstChild("leaderstats").Speed.Value += Value
end)

RemovteEvent.OnServerEvent:Connect(function(Player, Reward, Code)
	for Index,RedeemedCode in pairs(Player.RedeemedCodes:GetChildren()) do
		if RedeemedCode.Name == Code then
			print("already redeemed")
			return
		end
	end
		local Redeemed = Instance.new("BoolValue",Player.RedeemedCodes)
		Redeemed.Name = Code
		Redeemed.Value = true
	Player.leaderstats.Speed.Value += Reward
end)
1 Like