In this game im making, you’re given a random set upon joining. I’m starting off small making sure that only your set saves when you leave and get it back when you join.
I found this plugin where you press a button and it creates a script and a folder, in which I can put all the values that I want to save inside. It works somewhat as intended, when you leave and rejoin, you get your set, however whenever you die, you keep your set. Here’s the datastore script in SSS
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("Data#"..script.DataVersion.Value)
local saving = script["Saving"]
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local key = plr.UserId.."'s' Data"
local data = myDataStore:GetAsync(key)
print(data)
for _, folder in pairs(script:GetChildren()) do
if folder:IsA("Folder") then
local fc = folder:Clone()
fc.Parent = plr
char.Humanoid.Died:Connect(function()
print("you died")
myDataStore:RemoveAsync(plr.UserId.."'s' Data")
end)
if saving then
for _, item in pairs(fc:GetChildren()) do
if data then
item.Value = data[item.Name.." "..folder.Name]
else
warn("there is no data")
end
end
end
end
end
end)
end)
if saving then
local function create_table(plr)
local player_stats = {}
for _, folder in pairs(script:GetChildren()) do
if folder:IsA("Folder") then
for _, stat in pairs(plr:FindFirstChild(folder.Name):GetChildren()) do
player_stats[stat.Name.." "..folder.Name] = stat.Value
end
end
end
return player_stats
end
game.Players.PlayerRemoving:Connect(function(plr)
local player_stats = create_table(plr)
local succes, err = pcall(function()
local key = plr.UserId.."'s' Data"
myDataStore:SetAsync(key, player_stats)
end)
if succes then
print("saved")
else
warn(err)
end
end)
end
The first thing I did was underneath the humanoid.Died was try this script
char.Humanoid.Died:Connect(function()
print("you died")
fc:WaitForChild("HasData").Value = false
fc:WaitForChild("Set").Value = 0
myDataStore:SetAsync(plr.UserId.."'s' Data")
end)
However, I get this error, and decided to try to use RemoveAsync
Here’s my script for my set randomizer, also in SSS, this is currently both getting and setting the values of my data
local RS=game.ReplicatedStorage
local SS=game.ServerStorage
local can = true
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
game.ReplicatedStorage.Events.LoadEvent.Event:Connect(function(plr)
if plr.Name == Character.Name and can == true then
local hasdata = Player:FindFirstChild("Data"):FindFirstChild("HasData")
if hasdata.Value == false and can == true then -- checks for data
can = false
hasdata.Value = true
local bind = game.ReplicatedStorage.set2PD
local randomtoolfolder=RS.randomtool
local RTFchildren=randomtoolfolder:GetChildren()
local selectednumber=math.random(1, #RTFchildren)
local selectedfolder=RTFchildren[selectednumber]
for i,v in ipairs(selectedfolder:GetChildren())do
v:Clone().Parent=Player.Backpack
Player:FindFirstChild("Data"):FindFirstChild("Set").Value = selectedfolder.Name
end
local block
if selectednumber==4 then
block=SS.SBlocking:Clone()
local sjump = SS.SJump:Clone()
sjump.Parent = Character
else
block=SS.Blocking:Clone()
end
if block then
block.Parent=Player.Backpack
block.Disabled=false
end
if selectednumber==13 then
Character.Torso.Size = Vector3.new(2,2,2)
Character.Humanoid.MaxHealth = 250
Character.Humanoid.Health = Character.Humanoid.MaxHealth
Character.Humanoid.WalkSpeed -= 5
Character:FindFirstChild("Health"):Destroy()
local newhealth = SS.FatManHeal:Clone()
newhealth.Parent = Character
end
can = true
return
elseif hasdata.Value == true and can == true then
can = false
local datavalue = Player:FindFirstChild("Data"):FindFirstChild("Set").Value
local getfolder = RS.randomtool[datavalue]
for i,v in ipairs(getfolder:GetChildren())do
v:Clone().Parent=Player.Backpack
end
local block
if datavalue == "set4" then
block=SS.SBlocking:Clone()
local sjump = SS.SJump:Clone()
sjump.Parent = Character
else
block=SS.Blocking:Clone()
end
if block then
block.Parent=Player.Backpack
block.Disabled=false
end
if datavalue == "set13" then
Character.Torso.Size = Vector3.new(2,2,2)
Character.Humanoid.MaxHealth = 250
Character.Humanoid.Health = Character.Humanoid.MaxHealth
Character.Humanoid.WalkSpeed -= 5
Character:FindFirstChild("Health"):Destroy()
local newhealth = SS.FatManHeal:Clone()
newhealth.Parent = Character
end
can = true
return
end
end
end)
end)
end)
What ends up happening here is when you spawn in for the first time, your set is saved to HasData.Set.Value, however every time you die your set stays the same and you now get the same set of tools +1 every time you die. I had originally tried adding the “can” value as I thought it would stop the script from repeating, but even removing the can = true lines at the bottom of the ifs would make you spawn in with no tools after dying
First time loading in, works as intended (stomp isnt apart of the set and you get from other factors)
Dying for the first time
Dying for the third time
How can I get my datastore to clear on death or reset to a default value of “none” for the Set value and false for the HasData value, as well as stop my move randomizer from giving me the same set multiple times?