Untitled

 avatar
unknown
plain_text
a year ago
6.4 kB
19
Indexable
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local dataManager = require(ServerScriptService:WaitForChild("Modules"):WaitForChild("DataManager"))

local ApplyKnockback = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("ApplyKnockBack"))
local StunModule = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("StunModule"))

local CharacterModule = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("CharacterModule"))
local changeCharacter = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("ChangeCharacter")
local getCharacterInfo = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("getCharacterInfo")

local knockback = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("knockback")
local ragdoll = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("ragdoll")
local hit = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("hit")
local applyDamage = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("applyDamage")

local Config = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("Config"))
local AbilityModule = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("AbilityModule"))

local CharactersFolder = ReplicatedStorage:WaitForChild("Modules"):WaitForChild("CharacterModule"):WaitForChild("Characters")

local function applyAnimations(player: Player, character)
	local characterInstance = CharactersFolder:WaitForChild(character)
	
	if characterInstance then
		local charAnimate = characterInstance:FindFirstChild("Animate"):Clone()
		local playerAnimate = player.Character:WaitForChild("Animate")
		
		if charAnimate and playerAnimate then
			charAnimate.Parent = player.Character
			playerAnimate:Destroy()
		end
	else
		warn("Character instance not found for " .. character)
	end
end
	-- no extra end here ⬆️
local function applyCosmetics(player: Player, character)
	local characterInstance = CharactersFolder:WaitForChild(character)

	
if characterInstance then
	local accessories = characterInstance:FindFirstChild("Accessories")
	local tempFolder = player.Character:FindFirstChild("TempAccessories") or Instance.new("Folder")

	tempFolder.Name = "TempAccesories"
	tempFolder.Parent = player.Character

	for _, object in tempFolder:GetChildren() do
		object:Destroy()
	end

	if accessories then
		for _, accessoryFolder in ipairs(accessories:GetChildren()) do
			if accessoryFolder:IsA("Model") and accessoryFolder.Name == "AccessoryTemplateDummy" then
				for _, bodyPart in accessoryFolder:GetChildren() do
					local fakeBodyPart = bodyPart:Clone()
					fakeBodyPart.Position = player.Character[bodyPart.Name].Position
					fakeBodyPart.Orientation = player.Character[bodyPart.Name].Orientation 

					local weldConstraint = Instance.new("WeldConstraint")
						weldConstraint.Parent = fakeBodyPart
						weldConstraint.Part1 = fakeBodyPart
						weldConstraint.Part0 = player.Character[bodyPart.Name]

					fakeBodyPart.Transparency = 1
					fakeBodyPart.Parent = tempFolder
				end
			end
		end
	end
else
	warn("Character instance not found for " .. character)
end
end

local function applyCharacterStats(player, characterTable)
	player:SetAttribute("atk_dmg", characterTable.atk.dmg)
	player:SetAttribute("health", characterTable.health)
	player:SetAttribute("walkspeed_mult", characterTable.speedMult)

	local character = player.Character
	local hum = character:FindFirstChildOfClass("Humanoid")
	
	if hum then
	hum.WalkSpeed = Config.NormalWalkSpeed + characterTable.speedMult
	hum.MaxHealth = characterTable.health
	hum.Health = characterTable.health
	
	else
		warn("Humanoid not found in character")
	end
	
	task.wait(0.2)
	
	applyAnimations(player, characterTable.name)
	applyCosmetics(player, characterTable.name)

	AbilityModule:Cleanup(player)
	
	task.wait(0.2)
	
	AbilityModule.SetupCharacter(player, characterTable)

	
end

-- Player joins
Players.PlayerAdded:Connect(function(player)
	local characters = CharacterModule.GetCharacters(player)
	print(characters)

	local selectedCharacter = player:WaitForChild("selectedCharacter").Value
	local charInfo = CharacterModule.GetCharacterInfo(player, selectedCharacter)
	
	applyCharacterStats(player, charInfo)
end)

-- Change character
changeCharacter.OnServerEvent:Connect(function(player, character)
	if player and character then
		local profile = dataManager.Profiles[player]
		
		local charInfo = CharacterModule.GetCharacterInfo(player, character)

		if ReplicatedStorage:WaitForChild("Modules"):WaitForChild("CharacterModule"):WaitForChild("Characters"):FindFirstChild(character) then
			profile.Data.selectedCharacter = character
			player:WaitForChild("selectedCharacter").Value = profile.Data.selectedCharacter
			
			applyCharacterStats(player, charInfo)
		end
	end
end)

-- Get character info
getCharacterInfo.OnServerInvoke = function(player)
	local selectedChar = player:WaitForChild("selectedCharacter").Value
	local characterDataTable = CharacterModule.GetCharacterInfo(player, selectedChar)

	if selectedChar and characterDataTable then
		return selectedChar, characterDataTable
	else
		return nil, {}
	end
end

-- Knockback
knockback.OnServerEvent:Connect(function(player, EnemyCharacter: Model, Character: Model, Range: number, Height: number)
	local Knockback = ApplyKnockback.new(EnemyCharacter, Character, Range, Height)
	Knockback:Construct()
end)

-- Ragdoll
ragdoll.OnServerEvent:Connect(function(player, isRagdollValue, value)
	isRagdollValue.Value = value
end)

-- Hit / Stun
hit.OnServerEvent:Connect(function(player, EnemyCharacter, animationId, duration, option, hitOption, animationOption)
	if option == "STUN" then
		StunModule.StunFor(EnemyCharacter, duration)
	elseif option == "HIT" then
		StunModule.HitEffects(EnemyCharacter, animationId, hitOption, animationOption)
	elseif option == "BOTH" then
		StunModule.ApplyStunAndEffects(EnemyCharacter, duration, animationId, hitOption, animationOption)
	end
end)

-- Apply damage
applyDamage.OnServerEvent:Connect(function(player, hitChar: Model, damage)
	if hitChar then
		local hitHum = hitChar:FindFirstChildOfClass("Humanoid")
		hitHum:TakeDamage(damage)
	end
end)
Editor is loading...
Leave a Comment