I'm making a script where your random magic element saves/loads when you come back into the game but it's always a new element! Please help me with this script, thanks.
local myElementData = game:GetService("DataStoreService"):GetDataStore("ElementData") --Element Table Elements = {"Fire","Water","Wind","Lightning", "Earth"} --DataStore function onPlayerAdded(player) repeat wait() until player.Character local stats = Instance.new("StringValue") stats.Parent = player stats.Name = "stats" local myElement = Instance.new("StringValue") myElement.Parent = stats myElement.Name = "myElement" if myElementData:GetAsync("Element_"..player.Name) ~= nil then myElement.Value = myElementData:GetAsync(player.Name) print(myElement.Value) else --If it didn't load local RandomElement = math.random(1, #Elements) myElement.Value = Elements[RandomElement] print(myElement.Value) myElement.Value = myElementData:UpdateAsync(player.name) end myElement.Changed:connect(function(Val) myElementData:SetASync("Element "..player.Name, Val) end) end game.Players.PlayerAdded:connect(onPlayerAdded)
Line 26, myElement.Value = myElementData:UpdateAsync(player.Name)
does not supply the existing value. To fix this, you must use GetAsync(player.Name).
Upvote if this is helpful!