Particle enabled state DataStore

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

Are you placing the Particle Instance into a table? to save it to datastores?

There are many problems.

Firstly, you can’t save Instances to a DataStore.


Secondly,

you have a for loop constantly overwriting the DataStore, there are 2 problems

  1. Only the last iteration of the for loop will be saved (it keeps getting overwritting)
  2. You are making wayyy too many calls by looping and repeatedly called SetAsync (the DataStore will throttle)

Thirdly, even if you could save Instances to a DataStore, why? why are you trying to do that?

I’m pretty sure what you want to do is save whether the Particle Emitter is Enabled or not.

1 Like

Yes that’s my goal, to save the particle emitters enabled state.

So you’d should save table do the datastore, the table will need 2 things

  • A way to identify the Particle Emitter (else it will be turning the wrong ones on an off)
  • A bool value to know whether it should be enabled or not

If each Particle Emitter is uniquely named you can create a table like this

local table_to_save = {
       particle1 = true,         -- [ParticleEmitter.Name] as the key and [ParticleEmitter.Enabled] as the value
       particle2 = false,
}
1 Like

I want all the emitters to go off, for performance.

What you are trying to say is that you want to save whether all particle emitters should be on or off, not each individual particle emitter?

2 Likes

for that case just save to a datastore

true 
or 
false

Could you provide an example of where you mean to put that?

1 Like

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.

It’s a setting for each client. I’m asking how I should write the code.

That’s very vague, I’m understanding from that that you want me to write out the whole code for you.


You will need to know RemoteEvents and the Client-Server model

A very basic setup

-- 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)
1 Like