The problem is that I want to make the walkspeed of the player save when leaving(it can be upgraded during the game) and it does save, however, when the player rejoins and gets more speed that data will overwrite the pre- existing one without adding to it.
I would like to figure out a way to do add data to an already existing one without overwriting
local DataStoreService = game:GetService("DataStoreService")
local SpeedStore = DataStoreService:GetDataStore("SpeedStore")
local SpeedStoreEvent = game.ReplicatedStorage.SpeedStoreEvent
local speedFolder= game.ServerStorage:WaitForChild("SpeedFolder")
local data
game.Players.PlayerAdded:Connect(function(player)
local character = player.CharacterAdded:Wait()
local SpeedLevel = speedFolder:FindFirstChild(player.Name).Value
local data
SpeedLevel = data
local key = "Player_".. player.UserId
local success, errormessage = pcall(function()
data = SpeedStore:GetAsync(key)
end)
if success then
character.Humanoid.WalkSpeed = 32 + data*2
SpeedLevel = data
SpeedStoreEvent:FireClient(player, data)
elseif data == nil then
character.Humanoid.WalkSpeed = 32
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = "Player_".. player.UserId
local data = speedFolder:FindFirstChild(player.Name).Value
SpeedStore:SetAsync(key, data)
end)
I think it’s because you are storing the SpeedLevel in a variable. Since it’s a base type (number, string, or boolean) it will be copied to the variable, thus when editing SpeedLevel you are actually editing a copy and not the real thing. Instead use SpeedLevel.Value like so
game.Players.PlayerAdded:Connect(function(player)
local character = player.CharacterAdded:Wait()
local SpeedLevel = speedFolder:FindFirstChild(player.Name) -- not copied
local data
local key = "Player_".. player.UserId
local success, errormessage = pcall(function()
data = SpeedStore:GetAsync(key)
end)
if success then
character.Humanoid.WalkSpeed = 32 + data*2
SpeedLevel.Value = data -- using Value
SpeedStoreEvent:FireClient(player, data)
elseif data == nil then
character.Humanoid.WalkSpeed = 32
else
warn(errormessage)
end
end)
This will get you a reference which is of type table or a kind of instance. References change with each other where copies do not. Here is a more explicit example of copy vs references
Copy (number, string, boolean)
-- base type copy
local old_number: number = 100
local new_number: number = old_number
print(old_number, new_number) -- 100, 100
-- changing the new_number keeps the old
new_number = 50
print(old_number, new_number) -- 100, 50
Reference: (table, Instances)
-- table type reference
local old_table: {number} = {100, 80, 95}
local new_table: {number} = old_table
print(old_table[1], new_table[1]) -- 100, 100
-- changing the new table also changes the old
new_table[1] = 50
print(old_table[1], new_table[1]) -- 50, 50
It is a hidden feature that helps speed up games, I would say one of the most common mistakes in roblox! We call them “foot-guns” because it is powerful but very easy to miss and hit your foot!