Auto Equip Item Script

I’m trying to save the Player’s weapon when they die and have it Equip when the player spawns in after. I’ve been trying to save a Value into StarterCharacterScripts, but the Value does not change and just prints “-”. Any clue why?

local Players = game:GetService(“Players”)
local function onPlayerAdded(player)
local function onCharacterAdded(character)
local Sword = character.Sword.Value
local humanoid = character:WaitForChild(“Humanoid”)
print(Sword)

	local function onDied()
		local Sword = character.Sword
		local Tool = character:FindFirstChildOfClass("Tool")
		Sword.Value = tostring(Tool)
	end
	humanoid.Died:Connect(onDied)
end

player.CharacterAdded:Connect(onCharacterAdded)

end

Players.PlayerAdded:Connect(onPlayerAdded)

1 Like

local script (better put inside playerscripts)

local remoteToGiveWeapon = remote

Humanoid.Died:Connect(function()
      local Tool = --define tool here
      local toolName = Tool.Name
      remoteToGiveWeapon:FireServer(toolName)
end)

server script

--- you can add some security so player will not be able to spam with remote

--not sure but if it won't work try to add CharacterAdded INSIDE function

remote.OnServerEvent(player, toolName)
local toolName = tostring(toolName)
for i,v in pairs(folderWithTool:FindFirstChild(toolName)
      if i.Name == toolName then
      i:Clone().Parent = player.Backpack
end
end
end)

script can be wacky since i wrote it on phone

Would this in theory work with multiple tools? Or just one. Thanks for all the help btw

Yes it will work (thrtychrrrrs)

If @getdownhacktomen has solved your problem, please mark it as so.

Hmm your local script is successful in communicating what tool the player died with, however in the server script I’m getting the error “invalid argument #1 to ‘pairs’ (table expected, got Instance)” for the parameter I put (game.ServerScriptStorage.ShopItems:FindFirstChild(toolName)) “ShopItems” being the folder. Any ideas?

did you put ) at the end of pairs? Cus i forgot to (wait yeah actually you did, didn’t notice that)

Try to play with the in pairs() thing, cause i haven’t been using that function for a while