Hello everyone, I’m working on a lil’ survival sorta game. I had a question on how I should go about improving my script. I think this is good as is, but what if I say wanted a character to have a random weapon in a specified group (firearm - handgun / melee - axe)?
Script down below, feel free to advise things.
-- // SERVICES
local CollectionService = game:GetService("CollectionService")
local ServerStorage = game:GetService("ServerStorage")
-- // DEPENDENCIES
local assets = ServerStorage.Assets
local tools = assets.Tools
local spawns = workspace.Spawns:GetChildren()
local CharacterManager = {}
-- // Documentation or whatever
--[[
MaxHealth - The maximum amount of health the character can have.
MaxSanity - The maximum amount of sanity the character can have.
WalkSpeed - The character's walking speed.
SprintSpeed - The character's speed while sprinting.
DamageResistance - Damage multiplier.
HungerDecay - How fast the player goes hungry (per interval)
ThirstDecay - How fast the player goes thirsty (per interval)
HungerInterval - How often the player's hunger ticks down.
ThirstInterval - How often the player's thirst ticks down.
Tags - A list of tags to apply to the character.
StartingTools - A list of tool names to give the player when they spawn.
]]
CharacterManager.Characters = {
["Test"] = {
MaxHealth = 100,
MaxSanity = 100,
WalkSpeed = 16,
SprintSpeed = 21,
HungerDecay = 0.75,
ThirstDecay = 1,
HungerInterval = 7,
ThirstInterval = 5,
DamageResistance = 1,
Tags = {"test_tag_hiii"},
StartingTools = {"LinkedSword"}
}
}
local function TPPlayer(player: Player, character: Model)
local chosenSpawn = player:GetAttribute("Spawn")
local target
if #spawns == 0 then
warn("No spawns found!")
return
end
if chosenSpawn then
target = spawns[chosenSpawn] :: Part
character:MoveTo(target.Position)
else
target = spawns[math.random(1, #spawns)] :: Part
character:MoveTo(target.Position)
end
end
local function InitializeHumanoid(humanoid: Humanoid, characterData)
if humanoid then
humanoid.WalkSpeed = characterData.WalkSpeed
humanoid.MaxHealth = characterData.MaxHealth
humanoid.Health = humanoid.MaxHealth
for dataVar, value in pairs(characterData) do
if typeof(value) ~= "table" then
humanoid:SetAttribute(dataVar, value)
end
end
end
end
local function ApplyTags(character, characterData)
local dataTags = characterData.Tags
local existingTags = CollectionService:GetTags(character)
for _, tag in existingTags do
CollectionService:RemoveTag(character, tag)
end
if dataTags then
for _, tag in dataTags do
CollectionService:AddTag(character, tag)
end
end
end
local function GrantTools(player: Player, characterData)
local dataTools = characterData.StartingTools
local backpack = player:WaitForChild("Backpack")
if dataTools then
for _, toolName in dataTools do
if tools:FindFirstChild(toolName) then
local tool = tools[toolName]:Clone()
tool.Parent = backpack
end
end
end
end
function CharacterManager:LoadCharacter(player: Player)
local chosenChar = player:GetAttribute("Character")
local character = player.Character or player.CharacterAdded:Wait()
if chosenChar then
local characterData = self.Characters[chosenChar]
local humanoid = character:FindFirstChild("Humanoid") :: Humanoid
GrantTools(player, characterData)
InitializeHumanoid(humanoid, characterData)
ApplyTags(character, characterData)
TPPlayer(player, character)
else -- Random character
local keys = {}
for name, _ in pairs(self.Characters) do
table.insert(keys, name)
end
local randomKey = keys[math.random(1, #keys)]
local characterData = self.Characters[randomKey]
local humanoid = character:FindFirstChild("Humanoid") :: Humanoid
GrantTools(player, characterData)
InitializeHumanoid(humanoid, characterData)
ApplyTags(character, characterData)
TPPlayer(player, character)
player:SetAttribute("Character", randomKey)
end
end
return CharacterManager
