Need some help with this script

I’m trying to make a game where each time a player spawns in they are given a new gear, or whenever they die, they receive another one and lose the previous one. Like a gun-game, except not kill-based.

I was wondering how i could make this script work properly, I’m not too sure if it does but it keeps saying the "end"s are in the wrong place, but i’m not too sure how to correct that.

If anyone can help, it’d be much appreciated


function died(deadplayer)


game.Players.ChildAdded:connect(function(newPlayer)
		newPlayer.CharacterAdded:connect(function(newCharacter)
			local ffsdf = game.ReplicatedStorage.Tools:FindFirstChild(maps[math.random(1,#game.ReplicatedStorage.Tools)])
			ffsdf.Parent = newPlayer.Backpack
			
		end)
		
wait(0.01)
newCharacter.Humanoid.Died:connect(function() died(newCharacter) end)
			local j = newPlayer.Backpack:FindFirstChildOfClass("Tool")
			j:Destroy()
			local ffsdf = game.ReplicatedStorage.Tools:FindFirstChild(maps[math.random(1,#game.ReplicatedStorage.Tools)])
			ffsdf.Parent = newPlayer.Backpack
			
	end)
	
		
end

game.Players.PlayerAdded:connect(function(newPlayer)
     newPlayer.CharacterAdded:connect(function(newCharacter)
          local newTool = game.ReplicatedStorage.Tools:FindFirstChild(maps[math.random(1,#game.ReplicatedStorage.Tools)])
          newTool.Parent = newPlayer.Backpack	
     end)
end)

The code above should accomplish what you want. A couple of notes:

  1. You don’t need to remove the tools, this will be done automatically.
  2. The indents are kind of messy, so its hard to tell what’s going on.
  3. I changed ChildAdded to PlayerAdded. There’s not much difference but using PlayerAdded is better practice.
  4. ffsdf is probably a bad variable name to use. I changed it to newTool which is more descriptive.

If you’re interested, here’s a link to the tutorial on functions.

thank you for the help, i already managed to fix it but i appreciate it