I am trying to script a game where you survive very weird disasters such as hoards of cows or lava floors.
However, it’s quite common for players to lose parts in the game (for example, during a tornado it’s not uncommon for players to lose their arms) and sometimes they lose the ability to move.
However, I have programmed the game in a way that you are rewarded for not resetting - so I am trying to build a one-use gamepass item (by this i mean the player spawns with it if they have the gamepass, and then if they use it, its gone until they respawn) whose purpose is to regenerate the player’s limbs.
Here is my current code.
local tool = script.Parent.Parent
tool.Activated:Connect(function()
local a = game.Players:FindFirstChild(tool.Parent.Name)
a:LoadCharacter()
end)
Note that this code is inside of Handle.
Anyway, this technically works but it respawns the player, defeating the whole point of the item.
This doesn’t help that much but I would check if the player has lost any limbs once the tool is activated, if it is then it runs the code, I would also make a variable getting the health of the player once the tool is activated, so then once the player is losing a limb, it would respawn them but then you can make it so with the variable with the health inside of it, it sets the health to that much
Note: this isn’t the best option but I would try this first if I was making it
The easiest solution that I would do is just respawn the player and cframe/teleport him back to his original position. If you really want to make it look nice you could do a sort of effect like this:
Player uses item → Play an animation that shows the player ascending into the air with a beam of light covering his body (so they can’t see the actual respawn process) → Save the players original position → Set the cameras cframe to his current position → respawn the player → cframe the player to his original position → have an animation of the player descending back down with full limbs → set the camera subject back to the player.
Another option would be to manually create the players joints again (might be a bit more difficult, never tried).
Dumb question, doesn’t :load character() just reloads a character, or does it completely respawn them? I can really remember since I don’t use it that much.
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local CharacterSaveFolder = Instance.new("Folder")
CharacterSaveFolder.Name = "CharacterSaves"
CharacterSaveFolder.Parent = ServerStorage
local PreferredRigType = nil
local function GetRigType(Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
if Character then
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid then
return Humanoid.RigType
end
end
end
local function GetBaseParts(Saved)
local Parts = {}
for i, v in pairs(Saved:GetChildren()) do
if v:IsA("BasePart") then
table.insert(Parts, v)
end
end
return Parts
end
local function RestoreLimb(Character, Saved, Name)
local Limb = Saved:FindFirstChild(Name)
if Limb and not Character:FindFirstChild(Limb.Name) then
local NewLimb = Limb:Clone()
local Joint = NewLimb:FindFirstChildOfClass("Motor6D")
if Joint then
local Part0 = Joint.Part0
if Part0 then
local BasePart = Character:FindFirstChild(Part0.Name)
if not BasePart then
BasePart = RestoreLimb(Character, Saved, Part0.Name)
end
Joint.Part0 = BasePart
NewLimb.Parent = Character
return NewLimb
end
else
NewLimb:Destroy()
end
end
end
local function FindMissingParts(Saved, Character)
local Missing = {}
local x = GetBaseParts(Saved)
for i, v in pairs(x) do
local Part = Character:FindFirstChild(v.Name)
if not Part then
table.insert(Missing, v.Name)
end
end
return Missing
end
local function RestoreCharacter(Player)
local Saved = CharacterSaveFolder:FindFirstChild(Player.Name)
local Character = Player.Character
if Saved and Character then
local Missing = FindMissingParts(Saved, Character)
for i, v in pairs(Missing) do
RestoreLimb(Character, Saved, v)
end
end
end
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function()
local HumanoidDescription = Players:GetHumanoidDescriptionFromUserId(Player.UserId)
if HumanoidDescription then
local RigType = PreferredRigType or GetRigType(Player)
if RigType then
local Previous = CharacterSaveFolder:FindFirstChild(Player.Name)
if Previous then
Previous:Destroy()
end
local Model = Players:CreateHumanoidModelFromDescription(HumanoidDescription, RigType)
Model.Name = Player.Name
Model.Parent = CharacterSaveFolder
end
end
end)
end)
Players.PlayerRemoving:Connect(function(Player)
local Saved = CharacterSaveFolder:FindFirstChild(Player.Name)
if Saved then
Saved:Destroy()
end
end)
Find a way to connect the RestoreCharacter() function to your one time use gamepass.