How do I clear a table before new character scripts run?

  1. What do you want to achieve?
    I want to make a table that gets cleared when Player.CharacterAdded is fired, and all scripts have access to that table through a module script.

  2. What is the issue?
    The script that clears the table on Player.CharacterAdded clears the table after the character scripts run.

  3. What solutions have you tried so far?
    I tried duplicating the script that clears the table so it’ll run first, but I don’t think that’s an efficient way to deal with the problem.
    I also looked around the forum, but I think I’m just not phrasing it correctly.

image-1

I’m currently on my phone, so sorry if I made some typos or didn’t provide enough media.
Thanks for reading :slight_smile:

Why don’t you just disable the scripts that you don’t want to run at first then re-enable them?

Do games usually disable and re-enable scripts in such cases? I’ll try it out once I’ll be able to hop on Studio, but for now I’m just unsure how safe/efficient it is just because I barely ever re-enabled scripts in Roblox.

I’m not sure. But I’m pretty sure this should solve your current problem.

This certainly sounds weird, just by the title itself makes me want to say, use table.clear() for the ‘table’ you’re questioning, is there a more clear solution you’re trying to achieve?


Other things i’ve thought:

  • Using table.clear(table) can destroy all formats you originally had.

  • Duplicating a module script you’ve already required will keep it’s environment, while you’ve most likely already learned that, you can probably duplicate one that you haven’t required yet.


Other question, does this module exist within StarterCharacter or another place?

– Mostly curious because if it’s outside of it then you’ll have to update the character currently used within the module with the new character that was spawned

Yes, I do use table.clear() because I want to get rid of all of the stuff inside the table.
Its purpose is to store number values that will be summed together and be applied to the camera’s FieldOfView.

And no, the module script is stored inside the player’s PlayerScripts.
The problem is that the character scripts run before the script that clears the table, meaning that when a new number value is added to the table, it’ll just be cleared out right after.

Thinking about it, maybe I should just make a different module that gets added to the character just for that…

damn it so many typos

I’ve had a similar scenario before quite recently and it was due to the ‘clear’ function being called twice. But this is most likely not your case, although it does beg the question, is your table being cleared 2 times once before and another after?

Nope, it just clears it after the scripts inside the character manage to run before it.
Basically, the function that runs on Player.CharacterAdded runs after the character scripts run.

CharacterAdded runs slightly before any spawned / fully parented instances, had to deal with this a few hours ago regarding deleting a spawned instance, my solution however was putting a simple wait after confirming the wanted details in the thread. You could probably set up a method which waits and confirms that the new character has been fully loaded before running the script you want it to.

(Or reverse what I said although putting waits doesn’t sound like synchronous idea)

Found the solution! Apparently all I had to do is check if the UpdateCharacter function was called for the first time. (If it was called automatically when the script began running, not when CharacterAdded was fired.)

local function UpdateCharacter(NewCharacter,FirstTime)	
	if NewCharacter then
		
		-- Reset variables
		if not FirstTime then
			table.clear(HiddenCache)
			table.clear(CamCFrameOffsets)
			table.clear(FOVs)
			print("Cleared FOVs!",tick())
		end
		-- other stuff...
	end
end

UpdateCharacter(Player.Character,true) -- Check if Player.Character already exists.

Player.CharacterAdded:Connect(function(NewCharacter) -- Called  when the character resets, spawns in but Player.Character was nil, etc.
	UpdateCharacter(NewCharacter)
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.