How can I kill a function and all active functions or events inside of it?

Basically I want to be able to load data “slots” or “characters” where you select a character and it sets up events for the data of that character.

When you load a character it’ll call a function called loadSlot(slot) the slot being the data slot it loads obviously. But when the slot gets changed I want to completely kill the previous loadSlot function called and all events or active functions inside of it.

My current best way of doing this would to have the loadSlot() function be a script that I clone and then delete to stop all the running code. But maybe there is a better way?

1 Like

is cloning a script a viable way? Or should I avoid doing that?

so you want to load character slots, and using data with it, generally datastore would be useful here, if its a certain change like deleting the slot then u just wipe their data for that slot id think, unsure what type of change you’re referring to

2 Likes

its not changing data its loading a slot. It sets up events and stuff and has functions that can be called inside that change data.

-- Define the character data as a module
local characterData = {
  [1] = {
    name = "Character 1",
    -- Add any other data you need for this character
  },
  [2] = {
    name = "Character 2",
    -- Add any other data you need for this character
  },
  -- Add more character data as needed
}

-- Define the loadSlot function
local activeScript = nil

function loadSlot(slot)
  -- Clear any previous events or active functions here
  if activeScript then
    activeScript:Destroy()
    activeScript = nil
  end
  
  print("Loading slot", slot)
  
  -- Get the selected character's data
  local selectedCharacterData = characterData[slot]
  
  -- Create a new script to handle the selected character's data
  activeScript = Instance.new("Script")
  activeScript.Name = "ActiveScript"
  activeScript.Parent = workspace
  
  -- Do whatever you need to do with the character data here
end

-- Example usage: loadSlot(1) would load the data for Character 1

For the UI part just call the loadSlot function with the appropriate slot number.

2 Likes

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