Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Please Help! player.Character tool save ( Advanced ) ?

Ok so this is a really important script that I need for my game and I really want someone to help me with this.

This script basically saves your tools once you die and puts them in your inventory when you respawn, simple, but it only gets the tools from the players Backpack and not the character. So if I was holding a tool and died I wouldn't get it back. I've tried many things to fix this, but nothing worked. One time it worked when I used [ player.Character:getChildren() ] and cloned it when the player died, but then I realized that it also clones the player's legs and everything.

Also don't say "put your tools in StarterPack" because that is not what I need.

( Script is located in ServerScriptService as a server script )

function waitForSpawn(player)
	for i=1,100 do
		if player == nil then return false end
		if player.Character ~= nil and player.Character.Humanoid ~= nil and player.Character.Humanoid.Health ~= 0 then
			return true
		end
		wait(1)
	end
	return false 
end

---- Need Fixing Here v

function die(player, h)
	
	    if h.Health == 0 then
		local items = {}
		local backpackItems = player.Backpack:getChildren()
		
		for i=1,#backpackItems do
		table.insert(items, backpackItems[i]:clone())
		end

waitForSpawn(player)

		if player ~= nil then	
		wait(.1)	
		local backpackItems = player.Backpack:getChildren()
		for i=1,#backpackItems do
		backpackItems[i]:remove()
		end
		for i=1,#items do
		items[i].Parent = player.Backpack

end
end
end
end

---- Need Fixing Here ^

function aspawn(character)
	local h = character:findFirstChild("Humanoid")
	if h ~= nil then
		local player = game.Players:playerFromCharacter(character)
		if player ~= nil then
			h.Changed:connect(function () die(player, h) end)
		end
	end
end

game.Workspace.ChildAdded:connect(aspawn)

2 comments

1

You can use Instance:IsA("ClassName"). This function will check the instance in what class. For sample, you can loop though Children of the Character, and use simple if statement to check the Child is a tool or not. https://developer.roblox.com/en-us/api-reference/function/Instance/IsA

0

Like this: if child:IsA("Tool") then ...

1 answer

✓ Accepted answer

Issue

So if I was holding a tool and died I wouldn't get it back.

Solution

The Tool that is currently equipped by a player is stored in their Character. So when they die you look inside the character for an instance which is a Tool and you add it to the backpackItems list.

There is a useful method called FindFirstChildOfClass and another called FindFirstChildWhichIsA which will find the first child with the corresponding ClassName or an object that inherits the class passed.

I would use FindFirstChildOfClass but it does not matter.

https://developer.roblox.com/en-us/api-reference/function/Instance/FindFirstChildOfClass

https://developer.roblox.com/en-us/api-reference/function/Instance/FindFirstChildWhichIsA

Editing your code we could get something along the lines of: (I have also fixed your indentation as well as some other stuff)

function die(player, h)
    local character = player.Character
    local items = {}

    local currentTool = character:FindFirstChildOfClass("Tool")

    if (currentTool) then
        table.insert(items, currentTool)
    end

    local backpackItems = player.Backpack:GetChildren()

    for i, item in pairs(backpackItems) do
        table.insert(items, item)
    end

    waitForSpawn(player)

    if (player ~= nil) then   
        wait(.1)

        player.Backpack:ClearAllChildren()

        for i, item in pairs(items) do
            items.Parent = player.Backpack
        end
    end
end

2 comments

0

thanks it worked, but how do I accept answer?

0

You click "Accept Answer"

This discussion is preserved as it was. New answers, comments, and votes are closed.