Useful But Suspiciously Specific Coding Tips #2

You think you’re so smart, huh? Fixing every single small detail that you didn’t like in the previous part… Good luck on fixing something in this part.

Quick intro: Small idea I got not so long ago where I post coding tips on different random topics, count it as something funny that you would usually see on YouTube Shorts, (Previous Part: Useful but suspiciously specific coding tips)

Let’s start!

  1. Sometimes you need to save values as soon as certain condition is met. In this example, I have an item that boosts players stats when they hold it, but if player will leave the game with an item still in hands, it will mean that boosted stats will be perma-saved, SO, to deal with it, you can create a StringValue (if it’s multiple items like in my case), set the string’s value to the name of the tool, and when player leaves, check the strings name and what to do with it, and only then overwrite the stats:
game.Players.PlayerRemoving:Connect(function(player)
	if player.stats.StackedItemName.Value == "AFK" then -- check the name of a string value
		if player.stats.RandomIncomeOdds.Value < 95 then -- create a cap (in case of something will go wrong)

--set the stats back to their original values
			player.stats.RandomIncomeMax.Value = player.stats.RandomIncomeMax.Value - 5
			player.stats.RandomIncomeOdds.Value = player.stats.RandomIncomeOdds.Value + 25
			player.stats.RandomIncomeDecadenceOdds.Value = player.stats.RandomIncomeDecadenceOdds.Value + 25
			player.stats.RandomIncomeMaxDecadence.Value = player.stats.RandomIncomeMaxDecadence.Value - 5

--overwrite new values
			local ID = "RandomIncomeMax".."-"..player.UserId
			DataStore:SetAsync(ID,player.stats["RandomIncomeMax"].Value)
			local IDD = "RandomIncomeOdds".."-"..player.UserId
			DataStore:SetAsync(IDD,player.stats["RandomIncomeOdds"].Value)
			local IDDD = "RandomIncomeDecadenceOdds".."-"..player.UserId
			DataStore:SetAsync(IDDD,player.stats["RandomIncomeDecadenceOdds"].Value)
			local IDDDD = "RandomIncomeMaxDecadence".."-"..player.UserId
			DataStore:SetAsync(IDDDD,player.stats["RandomIncomeMaxDecadence"].Value)

			wait(5)
			print("AFK sign")

		end
	else
		print("I hate my life")
	end
end)
  1. Let’s Imagine a scenario where you need to :Clone() something somewhere, and then when the certain condition is met, delete that cloned thing (And believe me, wayyyy to many people don’t really know how to properly deal with it). So, the code that you could write would look like this:
local cln1 = GUI.Frame.effects.RIDO:Clone()

function onEquipped()
	GUI.Enabled = true
	cln1.Parent = GUI.Frame.ScrollingFrame
	cln1.Visible = true
	cln1.Name = "AFKEffect1"
	cln1.Text = "Random Income (Decadence) Chances: +15%"
end

function onDequipped()
	GUI.Frame.ScrollingFrame:FindFirstChild("AFKEffect1"):Destroy()
	GUI.Enabled = false
end

wroooooooooong, the thing with :Destroy() function is the fact that you can’t re-parent the same child again, even with :Clone(), so it wont work and in addition will also error, so, what you must do is to simple use :Remove() instead:

function onDequipped()
	GUI.Frame.ScrollingFrame:FindFirstChild("AFKEffect1"):Remove()
	GUI.Enabled = false
end

Yup, that’s it

  1. If you need to get country name and not a code (in my example i need to do it as a small jumoscare for my horror game, like, oh hey, i also know someone from Canada, how cool is that), there’s a code kindly contributed by @AC_Starmarine in here: How to get list of countries (not their codes) using LocalizationService - #6 by AC_Starmarine, and to get a value from a table, here’s a code im using:
local result, code = pcall(function()
	return LocalizationService:GetCountryRegionForPlayerAsync(player)
end)

print(regionCountries[code])

PS: LocalizationService can be used only in a local script, to pass this value to the server just simply use events!

5 Likes

Instance:Remove() is deprecated, and you should just parent the instance to nil for identical behavior:

function onDequipped()
	GUI.Frame.ScrollingFrame:FindFirstChild("AFKEffect1").Parent = nil
	GUI.Enabled = false
end
3 Likes

I read that using :Destroy() internally has more benefic outcomes than just parenting it to nil.

2 Likes

Yes, I was talking about :Remove(), not :Destroy().

2 Likes

The following code demonstrates how a part can be re-added to the DataModel after being removed:

1 Like