I have a setting button that will disable the particles if clicked. I’ve written this DataStore, and followed another DevForum topic but didn’t get a clear understanding of what’s going on. Whenever I disable the particles, leave, and join back they’re enabled again. Simple as that.
local DataStore = DataStoreService:GetDataStore("SettingsDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local DataToSave = {}
for i, Particle in pairs(game.Workspace:GetDescendants()) do
if Particle:IsA("ParticleEmitter") then
table.insert(DataToSave, Particle)
end
end
local Data
local Success, Error = pcall(function()
Data = DataStore:GetAsync(Player.UserId)
end)
if Data and Success then
for i, DataMain in pairs(DataToSave) do
DataMain.Enabled = Data.Enabled
end
else
warn("Failed to save data!")
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local DataToSave = {}
for i, Particle in pairs(game.Workspace:GetDescendants()) do
if Particle:IsA("ParticleEmitter") then
table.insert(DataToSave, Particle)
end
end
for i, DataMain in pairs(DataToSave) do
local Success, Error = pcall(function()
DataStore:SetAsync(Player.UserId, DataMain.Enabled)
end)
if Success then
print("Data saved!")
else
warn("Data failed to save!")
end
end
end)
Any Idea why this is happening?
Thanks,
- Skylexion
What are you asking? are you asking how to write the code to save to save something to a datastore, or how the player can tell the server if they want particles on or off?
I assume this a setting for each player that they can turn particles on or off.
-- Prerequisites
-- create a RemoteEvent and place it in ReplicatedStorage
LocalScript:
-- Local Script placed inside of a TextButton
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local button = script.Parent
local ParticleSetting = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("ParticleSetting")
if ParticleSetting == true then
-- turn on particles
else
-- turn off particles
end
button.MouseButton1Click:Connect(function()
if ParticleSetting == true then
RemoteEvent:Fire(false) -- tell the server we want particles off
-- turn off the particles
else
RemoteEvent:Fire(true) -- tell the server we want particles on
-- turn on the particles
end
end)
Server Script:
-- Server Script placed inside of ServerScriptService
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("ParticleSettings")
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
RemoveEvent.OnServerEvent:Connect(function(player,particles_on)
local ParticleSetting = player.leaderstats.ParticleSetting
ParticleSetting.Value = particles_on
end)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local ParticleSetting = Instance.new("BoolValue")
ParticleSetting.Name = "ParticleSetting"
ParticleSetting.Parent = leaderstats
local data
local success,err = pcall(function()
local key = player.UserId
data = DataStore:GetAsync(key) --> returns [bool]
end)
if success then
else
-- datastore call failed
player:Kick()
return
end
if data ~= nil then
ParticleSetting.Value = data -- remember [data] = boolean
else
ParticleSetting.Value = true -- or false whatever default value you want
then
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = player.UserId
local value = player.leaderstats.ParticleSetting.Value
local success, err = pcall(function()
DataStore:SetAsync(key,value)
end)
end)