Can you modify this server side script to make it so it refreshes the module script…
- User
Can you modify this server side script to make it so it refreshes the module script every time the player respawns? Some ability modules contain client scripts that need to be refreshed when the player dies and respawns
So when the player respawns, clone the ability module, then delete the old one, and place the new one in the player again after they respawn.
Here is the server script again: local ReplicatedStorage = game:GetService("ReplicatedStorage") local AbilitiesFolder = ReplicatedStorage:WaitForChild("Abilities") local EquipAbilityEvent = ReplicatedStorage:WaitForChild("EquipAbility") local AbilityActionEvent = ReplicatedStorage:WaitForChild("AbilityAction") local DataStoreService = game:GetService("DataStoreService") local Players = game:GetService("Players") local AbilityDataStore = DataStoreService:GetDataStore("PlayerAbilities") local playerAbilityCache = {}
local function LoadPlayerData(player) local success, data = pcall(function() return AbilityDataStore:GetAsync(player.Name .. "Abilities" .. player.UserId) end) if success and data then local ownedAbilities = {} local ownedAbilitiesFolder = Instance.new("Folder") ownedAbilitiesFolder.Name = "OwnedAbilities" ownedAbilitiesFolder.Parent = player for _, abilityName in ipairs(data.OwnedAbilities or {}) do for _, abilityModule in ipairs(AbilitiesFolder:GetChildren()) do if abilityModule:IsA("ModuleScript") and abilityModule:GetAttribute("Name") == abilityName then local ownedAbility = abilityModule:Clone() ownedAbility.Parent = ownedAbilitiesFolder break end end end playerAbilityCache[player.UserId] = { OwnedAbilities = ownedAbilities, LastEquippedAbility = data.LastEquippedAbility } local lastAbilityName = data.LastEquippedAbility if lastAbilityName then for _, ownedAbility in ipairs(ownedAbilitiesFolder:GetChildren()) do if ownedAbility:GetAttribute("Name") == lastAbilityName then for _, child in ipairs(player:GetChildren()) do if child:IsA("ModuleScript") and child.Name ~= "OwnedAbilities" then child:Destroy() end end local equippedAbility = ownedAbility:Clone() equippedAbility.Parent = player if equippedAbility:GetAttribute("Passive") then local abilityModule = require(equippedAbility) if abilityModule.Passive then abilityModule.Passive(player) end end break end end end else local ownedAbilitiesFolder = Instance.new("Folder") ownedAbilitiesFolder.Name = "OwnedAbilities" ownedAbilitiesFolder.Parent = player end end
local function SavePlayerData(player) local data = {} local ownedAbilities = {} local ownedAbilitiesFolder = player:FindFirstChild("OwnedAbilities") if ownedAbilitiesFolder then for _, abilityModule in ipairs(ownedAbilitiesFolder:GetChildren()) do local abilityName = abilityModule:GetAttribute("Name") if abilityName then table.insert(ownedAbilities, abilityName) end end end data.OwnedAbilities = ownedAbilities local equippedAbilityModule = nil for _, child in ipairs(player:GetChildren()) do if child:IsA("ModuleScript") and child.Name ~= "OwnedAbilities" then equippedAbilityModule = child break end end if equippedAbilityModule then data.LastEquippedAbility = equippedAbilityModule:GetAttribute("Name") end local success, err = pcall(function() AbilityDataStore:SetAsync(player.Name .. "" .. player.UserId, data) end) if not success then warn("Failed to save ability data for player " .. player.Name .. ": " .. err) end end
- Assistant