local dataservice = game:GetService('DataStoreService'):GetDataStore("example") game.Players.PlayerAdded:connect(function(player) ---------------------------------------------------------------- local ukey = 'id-'..player.userId local titanium = Instance.new('IntValue') --- Store titanium local resources = Instance.new('Folder',player) resources.Name = 'Resources' titanium.Parent = resources titanium.Name = 'Titanium' local GetSaved = dataservice:GetAsync(ukey) if GetSaved then titanium.Value = GetSaved[1] if titanium.Value ~= 0 then game.Lighting.Items.Titanium:Clone().Parent = player:WaitForChild("PlayerStorage") player.PlayerStorage.Titanium.Quantity.Value = titanium.Value end else local NumberForSaving = {titanium.Value} dataservice:SetAsync(ukey, NumberForSaving) end end) game.Players.PlayerRemoving:connect(function(player) local ukey = 'id-'..player.userId local savedtbl = {player.Resources.Titanium.Value} dataservice:SetAsync(ukey, savedtbl) end)
The script above It's my datastore script obviously... what i'm trying to do is when a player who joined the game titanium value does not equal to 0 a part from lighting will be clone to the player instance folder called "resources". so anyone who joined the game as long as their titanium value does not equal to 0 it will clone the part...
THE PROBLEM
If a player is new to the game and of course the player titanium value is 0 because the player hasn't played yet meaning the the part will not be cloned.. since the player titanium value is 0. Now if the player during the game gathered a titanium using a tool inside the tool i have a script which will add a value to the player titanium
Player.Resources.Titanium.Value = Player.Resources.Titanium.Value +5
Like that^, now i want to make it clone the part because the player titanium value is no longer equal to 0 and i want to clone the part only once just like when a player joins the game.
You essentially need an "OnChange" event handler (or perhaps a "SetTitanium" method) that will keep track of this for each player. Since you're creating the titanium
IntValue right there, it is easy to add an OnChange
to it:
if titanium.Value ~= 0 then game.ReplicatedStorage.Items.Titanium:Clone().Parent = player:WaitForChild("PlayerStorage") --note that I've changed Lighting to ReplicatedStorage; you should move your stuff there player.PlayerStorage.Titanium.Quantity.Value = titanium.Value else local con --must have this on its own line to reference it in the Changed function con = titanium.Changed:connect(function(prop) if prop ~= "Value" then return end --This is mainly here because some Roblox properties have multiple names and trigger the Changed event multiple times, but we don't want to SpawnPart more than once. if titanium.Value > 0 then con:disconnect() --we no longer need to listen to the Changed event game.ReplicatedStorage.Items.Titanium:Clone().Parent = player:WaitForChild("PlayerStorage") end end) end
As you can see, the idea is simply to keep track of the titanium value until it is non-zero. At that point, the player receives the cloned part. (You might want to put that cloning line into its own function to avoid duplication.)
Note: I do not update the PlayerStorage.Titanium.Quantity.Value in the OnChanged event. If you want that auomatically kept up-to-date, you can add it, but would then need to remove the con:disconnect()
part.
The note beside the local con
is saying that this wouldn't work:
local con = titanium.Changed:connect(function(prop) --con will be nil here, so calling con:disconnect() will create an error --This is because, when the function is being created, 'con' doesn't exist. Once the function is created and passed to the 'connect' function, *then* 'con' is created. end)