Afternoon dev forum peeps, got a bit of a meta question here but the gist is that i have a script which has a datastore in it for the values whenever the player leaves the game, what i want to try and do is make a gui button which will basically reset the datastore so the player can restart from square one.
heres my script:
---Putting a folder into the player with a bool value
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("theboy1234")
local questions = {
"Where are we?",
"Why am i here?",
"something even sillier",
"ghost",
"Tool",
"Tool2"
}
local actions = {
"pressed prompt",
"pressed prompt2",
"pressed button",
"has tool",
"has badge",
"has tool2",
"got the time!"
}
local pressedstuff = {
"pressed this"
}
game.Players.PlayerAdded:Connect(function(Player)
local booleans = Instance.new('Folder', Player)
booleans.Name = 'Booleans'
local Answered = Instance.new("Folder")
Answered.Name = "Answered"
Answered.Parent = booleans
local Actions = Instance.new("Folder")
Actions.Name = "Actions"
Actions.Parent = booleans
local PressedStuffs = Instance.new("Folder")
PressedStuffs.Name = "PressedStuffs"
PressedStuffs.Parent = booleans
for i, PresseStuff in pairs(pressedstuff) do
local booleanvalue = Instance.new("BoolValue")
booleanvalue.Name = PresseStuff
booleanvalue.Parent = PressedStuffs
end
for i, Action in pairs(actions) do
local booleanvalue = Instance.new("BoolValue")
booleanvalue.Name = Action
booleanvalue.Parent = Actions
end
local success, dataofuser = pcall(function()
return ds:GetAsync(Player.UserId)
end)
for i, Question in pairs(questions) do
local booleanvalue = Instance.new("BoolValue")
booleanvalue.Name = Question
booleanvalue.Parent = Answered
end
if dataofuser ~= nil then -- anything but nil
warn(dataofuser)
print(dataofuser)
for i, k in pairs(dataofuser) do
for i, v in ipairs(k.Booleans) do
booleans[k.name][v.name].Value = v.value
end
end
else
print(success, dataofuser)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local datasave = {}
for n, v in ipairs(plr.Booleans:GetChildren()) do
table.insert(datasave, {name = v.Name, Booleans = {}})
for i, v in ipairs(v:GetChildren()) do
table.insert(datasave[n].Booleans, {name = v.Name, value = v.Value})
end
end
print(datasave)
local success, response = pcall(function()
ds:SetAsync(plr.UserId, datasave)
end)
if success then
print("succesfully saved data of " .. plr.Name)
else
warn(response)
end
end)
You would do this by using a remote event so that you can communicate from the client to the server that you want to reset your data.
Add a new RemoteEvent to ReplicatedStorage named ResetData:
Add a new LocalScript under your button, and paste this inside:
--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables
local ResetData = ReplicatedStorage.ResetData
local Button = script.Parent
--//Functions
Button.MouseButton1Down:Connect(function()
ResetData:FireServer()
end)
Replace your current script with this:
--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables
local ds = DataStoreService:GetDataStore("theboy1234")
local ResetData = ReplicatedStorage.ResetData
--//Tables
local questions = {
"Where are we?",
"Why am i here?",
"something even sillier",
"ghost",
"Tool",
"Tool2"
}
local actions = {
"pressed prompt",
"pressed prompt2",
"pressed button",
"has tool",
"has badge",
"has tool2",
"got the time!"
}
local pressedstuff = {
"pressed this"
}
--//Functions
local function SaveData(player, data)
local success, errorMessage = pcall(function()
ds:SetAsync(player.UserId, data)
end)
if not success then
warn(errorMessage)
end
end
local function OnPlayerRemoving(player)
local datasave = {}
for n, v in player.Booleans:GetChildren() do
table.insert(datasave, {name = v.Name, Booleans = {}})
for i, v in v:GetChildren() do
table.insert(datasave[n].Booleans, {name = v.Name, value = v.Value})
end
end
SaveData(player, datasave)
print(datasave)
end
ResetData.OnServerEvent:Connect(function(player)
player:Kick("Your data has been reset.")
ds:RemoveAsync(player.UserId)
end)
Players.PlayerAdded:Connect(function(Player)
local booleans = Instance.new('Folder')
booleans.Name = 'Booleans'
booleans.Parent = Player
local Answered = Instance.new("Folder")
Answered.Name = "Answered"
Answered.Parent = booleans
local Actions = Instance.new("Folder")
Actions.Name = "Actions"
Actions.Parent = booleans
local PressedStuffs = Instance.new("Folder")
PressedStuffs.Name = "PressedStuffs"
PressedStuffs.Parent = booleans
for i, PresseStuff in pressedstuff do
local booleanvalue = Instance.new("BoolValue")
booleanvalue.Name = PresseStuff
booleanvalue.Parent = PressedStuffs
end
for i, Action in actions do
local booleanvalue = Instance.new("BoolValue")
booleanvalue.Name = Action
booleanvalue.Parent = Actions
end
for i, Question in questions do
local booleanvalue = Instance.new("BoolValue")
booleanvalue.Name = Question
booleanvalue.Parent = Answered
end
local success, dataofuser = pcall(function()
return ds:GetAsync(Player.UserId)
end)
if dataofuser then -- anything but nil
print(dataofuser)
for i, k in dataofuser do
for i, v in k.Booleans do
booleans[k.name][v.name].Value = v.value
end
end
else
print(success, dataofuser)
end
end)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
game:BindToClose(function()
for i, player in Players:GetPlayers() do
OnPlayerRemoving(player)
end
end)
I have also added a feature you forgot to add, saving when the server is restarted.
Okay so i followed your steps word for word, but it still seems the value i set to true is still set to true after pressing the button and it kicking you, any way i could bugfix this?